Bill Walton
2006-Mar-09 16:59 UTC
[Rails] Need help PLEASE!!! - updating collection from form
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") %> <table cellpadding="5" cellspacing="0"> <% odd_or_even = 0 for @illness in @illnesses odd_or_even = 1 - odd_or_even %> <tr valign="top" class = "Listline<%= odd_or_even %>"> <td width="200"> <%=h @illness.illness %> </td> <td width="200"> <%= options = [["Select a qualifier", ""]] + Illness::DATE_QUALIFIERS select("illness[]", "qualifier", options) %> </td> <td width="200"> <%= options = [["Select a year", ""]] + Illness::DATE_YEARS select("illness[]", "illness_date", options) %> </td> </tr> <% end %> </table> <hr/> <input type = "submit" value = "Update" /> <%= 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"}}} -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060309/2ba4c421/attachment.html
Alan Francis
2006-Mar-10 10:13 UTC
[Rails] Re: Need help PLEASE!!! - updating collection from form
> 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. -- Posted via http://www.ruby-forum.com/.
Alan Francis
2006-Mar-10 10:28 UTC
[Rails] Re: Need help PLEASE!!! - updating collection from form
Alan Francis 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. -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Mar-10 13:07 UTC
[Rails] Re: Need help PLEASE!!! - updating collection from form
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 Francis" <alancfrancis@gmail.com> To: <rails@lists.rubyonrails.org> Sent: 2006-03-10 4:28 AM Subject: [Rails] Re: Need help PLEASE!!! - updating collection from form> Alan Francis 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails