# Simple variable question

**URL:** <https://www.ruby-forum.com/t/simple-variable-question/116723>\
**Category:** Rails\
**Created:** [October 9, 2007, 12:35am UTC](https://www.ruby-forum.com/t/simple-variable-question/116723 "2007-10-09T00:35:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2007, 12:35am UTC](https://www.ruby-forum.com/t/simple-variable-question/116723/1 "2007-10-09T00:35:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2007, 1:12am UTC](https://www.ruby-forum.com/t/simple-variable-question/116723/2 "2007-10-09T01:12:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/simple-variable-question/116723/3 "2007-10-09T12:40:19Z")

</div>

> 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
