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!

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 :slight_smile:

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.

ic ould also go with user.params[:field] = params[:value], then again
no such method error

user[params[:field]] should change into user[“name”] so

user[params[:field]]=params[:value]
user.save

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs