Hi, I''m new in RoR and i wrote this mini-app, which can to add, edit and delete the item in/from database. I think the edit and add item isn''t solve very good. I''m making both operations during one form. CONTROLLER: def create if params[:name].length > 0 then if params[:id_obj].length > 1 then #editing of current record @update=Person.find(params[:id_obj]) if @update.update_attributes(:name=>params[:name]) flash[:notice] = ''Your record was succesfly edited.'' redirect_to :action => ''index'' else flash[:warning] = ''ERROR: your record was not edited (no input)!'' redirect_to :action => ''index'' end else #creating the new record @haha=Person.new(:name=>params[:name]) if @haha.save flash[:notice] = ''Your record was succesfly saved.'' redirect_to :action => ''index'' else flash[:warning] = ''ERROR: your record was not succesfly saved.'' redirect_to :action => ''index'' end end else #input for name is empty flash[:warning] = ''ERROR: your record was not succesfly saved (no input).'' redirect_to :action => ''index'' end end VIEW: <%= form_tag({ :action => ''create'' }, { :method => ''post'' }) do %> <table> <tr> <td colspan="2">Here put your own idea! :)</td> </tr> <tr> <td colspan="2"><%= hidden_field_tag ''id_obj'', if @update then @update.id else '''' end %></td> </tr> <tr> <td>Name: </td> <td><%= text_field_tag ''name'', if @update then @update.name else '''' end %></td> </tr> <tr> <td>Text of message: </td> <td><textarea name="text"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" name="send" value="Save this post!" /></td> </tr> </table> <% end %> So I would to ask you about help, how to make this code more effective -- this solve is not much nice... Thanks, M. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Mar 9, 2011 at 6:51 AM, Manny 777 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I''m new in RoR and i wrote this mini-app, which can to add, edit and > delete the item in/from database. I think the edit and add item isn''t > solve very good. I''m making both operations during one form. > >try generating a model using a scaffold and see how the view and controller actions talk with each other. there you''ll see what you need to change in your mini app. good luck!> CONTROLLER: > def create > if params[:name].length > 0 then > > if params[:id_obj].length > 1 then #editing of current record > @update=Person.find(params[:id_obj]) > > if @update.update_attributes(:name=>params[:name]) > flash[:notice] = ''Your record was succesfly edited.'' > redirect_to :action => ''index'' > else > flash[:warning] = ''ERROR: your record was not edited (no > input)!'' > redirect_to :action => ''index'' > end > > else #creating the new record > @haha=Person.new(:name=>params[:name]) > > if @haha.save > flash[:notice] = ''Your record was succesfly saved.'' > redirect_to :action => ''index'' > else > flash[:warning] = ''ERROR: your record was not succesfly > saved.'' > redirect_to :action => ''index'' > end > end > > else #input for name is empty > flash[:warning] = ''ERROR: your record was not succesfly saved (no > input).'' > redirect_to :action => ''index'' > end > end > > VIEW: > <%= form_tag({ :action => ''create'' }, { :method => ''post'' }) do %> > <table> > <tr> > <td colspan="2">Here put your own idea! :)</td> > </tr> > <tr> > <td colspan="2"><%= hidden_field_tag ''id_obj'', if @update then > @update.id else '''' end %></td> > </tr> > <tr> > <td>Name: </td> > <td><%= text_field_tag ''name'', if @update then @update.name else > '''' end %></td> > </tr> > <tr> > <td>Text of message: </td> > <td><textarea name="text"></textarea></td> > </tr> > <tr> > <td></td> > <td><input type="submit" name="send" value="Save this post!" > /></td> > </tr> > </table> > <% end %> > > So I would to ask you about help, how to make this code more effective > -- this solve is not much nice... > > Thanks, M. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
What about using an CRUD controller? On 9 mar, 07:51, Manny 777 <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I''m new in RoR and i wrote this mini-app, which can to add, edit and > delete the item in/from database. I think the edit and add item isn''t > solve very good. I''m making both operations during one form. > > CONTROLLER: > def create > if params[:name].length > 0 then > > if params[:id_obj].length > 1 then #editing of current record > @update=Person.find(params[:id_obj]) > > if @update.update_attributes(:name=>params[:name]) > flash[:notice] = ''Your record was succesfly edited.'' > redirect_to :action => ''index'' > else > flash[:warning] = ''ERROR: your record was not edited (no > input)!'' > redirect_to :action => ''index'' > end > > else #creating the new record > @haha=Person.new(:name=>params[:name]) > > if @haha.save > flash[:notice] = ''Your record was succesfly saved.'' > redirect_to :action => ''index'' > else > flash[:warning] = ''ERROR: your record was not succesfly > saved.'' > redirect_to :action => ''index'' > end > end > > else #input for name is empty > flash[:warning] = ''ERROR: your record was not succesfly saved (no > input).'' > redirect_to :action => ''index'' > end > end > > VIEW: > <%= form_tag({ :action => ''create'' }, { :method => ''post'' }) do %> > <table> > <tr> > <td colspan="2">Here put your own idea! :)</td> > </tr> > <tr> > <td colspan="2"><%= hidden_field_tag ''id_obj'', if @update then > @update.id else '''' end %></td> > </tr> > <tr> > <td>Name: </td> > <td><%= text_field_tag ''name'', if @update then @update.name else > '''' end %></td> > </tr> > <tr> > <td>Text of message: </td> > <td><textarea name="text"></textarea></td> > </tr> > <tr> > <td></td> > <td><input type="submit" name="send" value="Save this post!" > /></td> > </tr> > </table> > <% end %> > > So I would to ask you about help, how to make this code more effective > -- this solve is not much nice... > > Thanks, M. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.