Need help PLEASE! - updating collection from form


#1

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:inupdate_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:inperform_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:inmeasure’
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:inperform_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:inprocess’
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:inhandle_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:inservice’
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:instart_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:instart’
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:instart’
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:indispatch’
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:inrequire’
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:inrequire’
script/server:3#{RAILS_ROOT}/app/controllers/phistoryadmin_controller.rb:22:in
update_illness' #{RAILS_ROOT}/app/controllers/phistoryadmin_controller.rb:21:inupdate_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:inperform_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:inmeasure’
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:inperform_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:inprocess’
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:inhandle_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:inservice’
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:instart_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:instart’
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:instart’
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:indispatch’
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:inrequire’
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:inrequire’
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”}}}


#2
  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.


#3

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.


#4

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
To: removed_email_address@domain.invalid
Sent: 2006-03-10 4:28 AM
Subject: [Rails] Re: Need help PLEASE!!! - updating collection from form