andresmax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-08 22:33 UTC
simple variable question
Hey everyone! Just have a quick question, im writing a method to update a field specified through an ajax edit in place, (i dont want to write one method for every single field, so im going more generic) currently im receiving the field name in params[:field] i want to do something like @user.update_attributes(:params[:field]=>:params[:value]) this wont work, with or without the : i will get a method or parameter error... how can i access the variable data? ic ould also go with user.params[:field] = params[:value], then again no such method error thnx in advance! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi What gets passed back from the form is params which is a hash. So when you say params[:value] you are asking for the value associated with the key :value. Params will look something like this: {:field => "name", :value => "john"} so true - params[:field] == "name" true - params[:value == "john" the way update_attributes works is it expects a hash but it expects a hash that in this case would look like this: {:name => "john"} so if you were to say User.update_attributes({:name => "john") it should work. you could also use update_attribute (singular which will do what you are expecting. (I could be grossly mistaken about all of this - its late and the coffee machine stopped working :) regards ivor btw, write some garbage at the first line after "def actionname" in your action that you post to and check the log - it will show you what params looks like. On 10/9/07, andresmax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andresmax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hey everyone! Just have a quick question, im writing a method to > update a field specified through an ajax edit in place, (i dont want > to write one method for every single field, so im going more generic) > > currently im receiving the field name in params[:field] i want to do > something like > > @user.update_attributes(:params[:field]=>:params[:value]) > > this wont work, with or without the : i will get a method or parameter > error... how can i access the variable data? > > ic ould also go with user.params[:field] = params[:value], then again > no such method error > > thnx in advance! > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> ic ould also go with user.params[:field] = params[:value], then again > no such method erroruser[params[:field]] should change into user["name"] so user[params[:field]]=params[:value] user.save -- 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 -~----------~----~----~----~------~----~------~--~---