# Need help PLEASE! - updating collection from form

**URL:** <https://www.ruby-forum.com/t/need-help-please-updating-collection-from-form/79087>\
**Category:** Rails\
**Created:** [March 9, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/need-help-please-updating-collection-from-form/79087 "2006-03-09T18:01:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 9, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/need-help-please-updating-collection-from-form/79087/1 "2006-03-09T18:01:37Z")

</div>

Greetings!

I’m trying to update a set of records from the same model on one form.  
The AWD book gives an example on p.356 but I guess I’m not understanding  
it.

I can see from the error message I’m getting that the controller is  
receiving a hash of hashes back from the view, and that it’s got the  
stuff I expected (two values per record that was sent to the view)

What I can’t figure out is how to update the records I sent in, based on  
the values returned. I would _really_ appreciate it if someone could  
help point me in the right direction. I’ve included the view and  
controller code below.

Thanks in advance for any assistance!  
Bill

--------------------- controller code ------------------------  
def edit\_illnesses  
# send a collection of records to the view  
@emrec = find\_emrec  
@illnesses = Illness.find(:all,  
:conditions =\> [“emrec\_id = ?”,  
@emrec.id])  
end

def update\_illness

# update the records sent in ‘edit\_illnesses’ based on what’s

returned in ‘params’  
@emrec = find\_emrec  
@illnesses = Illness.find(:all,  
:conditions =\> [“emrec\_id = ?”,  
@emrec.id])  
for i in @illnesses  
# I can’t figure out what should go here to update each of the  
records  
# with the new content returned from the form. How do I pull  
the  
# data from the params hash of hashes? The current code is:  
i.update(params[:illness].id, (params[:illness].id).illness\_date,  
(params[:illness].id).qualifier)  
i.save  
end  
redirect\_to :controller =\> ‘creation’, :action =\> ‘show\_emrec’  
end

---------------------- end controller code -------------------------

------------------ view code --------------------------------  
\<%= form\_tag(:action =\> “update\_illness”) %\>

\<%  
odd\_or\_even = 0  
for @illness in @illnesses  
odd\_or\_even = 1 - odd\_or\_even  
%\>

"\> \<% end %\>

| \<%=h @illness.illness %\> | \<%= options = [["Select a qualifier", ""]] + Illness::DATE\_QUALIFIERS select("illness[]", "qualifier", options) %\> | \<%= options = [["Select a year", ""]] + Illness::DATE\_YEARS select("illness[]", "illness\_date", options) %\> |

* * *
\<%= end\_form\_tag %\>

----------------------- end view code ----------------------

----------------------- error message -----------------------  
NoMethodError in Phistoryadmin#update\_illness  
undefined method `illness\_date’ for 32200148:FixnumRAILS\_ROOT:  
./script/…/config/…

Application Trace | Framework Trace | Full Trace  
#{RAILS\_ROOT}/app/controllers/phistoryadmin\_controller.rb:22:in  
`update_illness' #{RAILS_ROOT}/app/controllers/phistoryadmin_controller.rb:21:in`update\_illness’C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/base.rb:853:in  
`perform_action_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in`perform\_action\_without\_benchmark’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in`perform\_action’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/base.rb:369:in  
`process_without_session_management_support' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in`process’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in  
`dispatch' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in`handle\_dispatch’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick\_server.rb:83:in  
`service' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:95:in`start’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:23:in`start’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in`dispatch’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59  
C:/INSTAN~1/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in`require’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28  
C:/INSTAN~1/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in`require’  
script/server:3#{RAILS\_ROOT}/app/controllers/phistoryadmin\_controller.rb:22:in  
`update_illness' #{RAILS_ROOT}/app/controllers/phistoryadmin_controller.rb:21:in`update\_illness’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/base.rb:853:in  
`perform_action_without_filters' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in`perform\_action\_without\_benchmark’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in`perform\_action’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/base.rb:369:in  
`process_without_session_management_support' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in`process’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in  
`dispatch' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in`handle\_dispatch’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick\_server.rb:83:in  
`service' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:95:in`start’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start' C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:23:in`start’  
C:/INSTAN~1/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in`dispatch’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59  
C:/INSTAN~1/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in`require’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28  
C:/INSTAN~1/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in`require’  
script/server:3Request  
Parameters: {“illness”=\>{“11”=\>{“illness\_date”=\>“1970”,  
“qualifier”=\>“not since”}, “9”=\>{“illness\_date”=\>“1972”,  
“qualifier”=\>“since”}, “10”=\>{“illness\_date”=\>“1971”, “qualifier”=\>“not  
since”}}}

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 10, 2006, 11:13am UTC](https://www.ruby-forum.com/t/need-help-please-updating-collection-from-form/79087/2 "2006-03-10T11:13:40Z")

</div>

> ```
> i.update(params[:illness].id, (params[:illness].id).illness_date, 
> 
> ```
> 
> (params[:illness].id).qualifier)

> Parameters: {“illness”=\>{“11”=\>{“illness\_date”=\>“1970”,  
> “qualifier”=\>“not since”}, “9”=\>{“illness\_date”=\>“1972”,  
> “qualifier”=\>“since”}, “10”=\>{“illness\_date”=\>“1971”, “qualifier”=\>“not  
> since”}}}

You access the hash used for update as:

params[:illness][id]

e.g. params[:illness][“11”] gives {“illness\_date”=\>“1970”,  
“qualifier”=\>“not since”}

Does that help ?

A.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 10, 2006, 11:28am UTC](https://www.ruby-forum.com/t/need-help-please-updating-collection-from-form/79087/3 "2006-03-10T11:28:47Z")

</div>

Alan F. wrote:

> > ```
> > i.update(params[:illness].id, (params[:illness].id).illness_date, 
> > 
> > ```
> > 
> > (params[:illness].id).qualifier)
> 
> > Parameters: {“illness”=\>{“11”=\>{“illness\_date”=\>“1970”,  
> > “qualifier”=\>“not since”}, “9”=\>{“illness\_date”=\>“1972”,  
> > “qualifier”=\>“since”}, “10”=\>{“illness\_date”=\>“1971”, “qualifier”=\>“not  
> > since”}}}
> 
> You access the hash used for update as:
> 
> params[:illness][id]
> 
> e.g. params[:illness][“11”] gives {“illness\_date”=\>“1970”,  
> “qualifier”=\>“not since”}

Thinking about this, I’m more than a little sketchy, but I think  
something like:

params[:illness].each\_pair do |key, value|  
Illness.find(key).update(value)  
end

should work.

A.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 10, 2006, 2:08pm UTC](https://www.ruby-forum.com/t/need-help-please-updating-collection-from-form/79087/4 "2006-03-10T14:08:34Z")

</div>

Hi Alan,

Thanks very much for your replies. I did get this working yesterday  
using  
code very much like what you suggest here. My original intent was to  
start  
with the records and search the params. I later decided to try the  
reverse.  
Thus the two seperate pleas. At any rate, in the end it was easy. All  
except for finding the documentation ;-(

I think maybe I’ll put up a wiki page on accessing the params hash for  
other  
nubies.

Thanks again,  
Bill

----- Original Message -----  
From: “Alan F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: 2006-03-10 4:28 AM  
Subject: [Rails] Re: Need help PLEASE!!! - updating collection from form
