Hello I am a Ruby newbie, thus a rather simple question. My list view shows table records and enables user to click a button in order to change value of one attribute in given record. I use this code to invoke a method from controller: <%= button_to "Change", { :action => "edit", :id => prog.id_prog } %> My method looks like this (just a simple one - to set the salary attribute to 1000) def edit prog = Prog.find(params[:id]) prog.update_attribute(:salary, 1000) redirect_to :action => ''list'' end However after a list view is shown again the change is not made to the database. I do not get it, as find returns the proper record and update_attribute returns true. What may be wrong? Thanks in advance -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ptx wrote:> Hello > > I am a Ruby newbie, thus a rather simple question. > My list view shows table records and enables user to click a button in > order to change value of one attribute in given record. I use this code > to invoke a method from controller: > > <%= button_to "Change", { :action => "edit", :id => prog.id_prog } %> > > My method looks like this (just a simple one - to set the salary > attribute to 1000) > > def edit > prog = Prog.find(params[:id]) > prog.update_attribute(:salary, 1000)> redirect_to :action => ''list'' > end > > However after a list view is shown again the change is not made to the > database. I do not get it, as find returns the proper record and > update_attribute returns true. What may be wrong? > > Thanks in advance >I believe you need a ''prog.save'' in there, before the redirect_to --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 10, 2007, at 11:17 AM, Benjamin Ritcey wrote:> Ptx wrote: >> Hello >> >> I am a Ruby newbie, thus a rather simple question. >> My list view shows table records and enables user to click a >> button in >> order to change value of one attribute in given record. I use this >> code >> to invoke a method from controller: >> >> <%= button_to "Change", { :action => "edit", :id => prog.id_prog } %> >> >> My method looks like this (just a simple one - to set the salary >> attribute to 1000) >> >> def edit >> prog = Prog.find(params[:id]) >> prog.update_attribute(:salary, 1000) > >> redirect_to :action => ''list'' >> end >> >> However after a list view is shown again the change is not made to >> the >> database. I do not get it, as find returns the proper record and >> update_attribute returns true. What may be wrong? >> >> Thanks in advance >> > > I believe you need a ''prog.save'' in there, before the redirect_toNo, update_attribute automatically saves the record: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000909 Does the code work when executed from the console? One thing you might like to try is logging an inspection of the prog object to see if that gives any insight: def edit prog = Prog.find(params[:id]) prog.update_attribute(:salary, 1000) # Something like: logger.warn prog.inspect redirect_to :action => ''list'' end James. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
update_attribute() calls save(), but remember that save() can return false on failure. Test the return value of prog.update_attribute(:salary, 1000). You''ll probably find that it''s false, which means the save() failed. Then you can start looking for _why_ it failed. However, this is a good lesson. _Always_ test for save failure in actions that call it. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Benjamin Ritcey wrote:> Ptx wrote: >> def edit >> prog = Prog.find(params[:id]) >> prog.update_attribute(:salary, 1000) > >> redirect_to :action => ''list'' >> end >> I believe you need a ''prog.save'' in there, before the redirect_toThis does not solve the problem. Still the update has no effect. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sheldon Hearn wrote:> Test the return value of prog.update_attribute(:salary, 1000). You''ll > probably find that it''s false, which means the save() failed.update_attribute returns true. I checked it already as mentioned in first post. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Another hint - update_all() works fine, however it is a bit neat to update in that way, so if anyone has any more suggestions, I will be grateful. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---