Larry Kelly
2005-Jun-13 19:50 UTC
validations not working with collection population droplists
I have an ADD form with foreign key field values selectable via select list ( collection_select (...)). When I add validates_presence_of :someotherfield to my model for error trapping, I get the following error for each droplist: NoMethodError in Reviews#create_curriculum Showing /reviews/_form.rhtml where line #16 raised: undefined method `inject'' for nil:NilClass Extracted source (around line #16): 13: <tr> 14: <td class="menutitle">Who published it?:</td> 15: <td> 16: <%= collection_select ''review'', ''publisher_id'', @publishers, ''id'', ''name'' %> 17: <%= link_to ''Add'', :controller => ''publishers'', :action => ''new'' %> 18: </td> 19: </tr> Trace of template inclusion: /reviews/new_curriculum.rhtml Show template trace /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/helpers/form_options_helper.rb:139:in `options_from_collection_for_select'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/helpers/form_options_helper.rb:304:in `to_collection_select_tag'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/helpers/form_options_helper.rb:66:in `collection_select'' (erb):16:in `evaluate_locals'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:265:in `evaluate_locals'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:276:in `rhtml_render'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:201:in `send'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:201:in `render_template'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:181:in `render_file'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:195:in `render'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/partials.rb:51:in `render_partial'' (erb):3:in `evaluate_locals'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:265:in `evaluate_locals'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:276:in `rhtml_render'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:201:in `send'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:201:in `render_template'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_view/base.rb:181:in `render_file'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/layout.rb:208:in `render_without_benchmark'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/benchmarking.rb:23:in `render'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/benchmarking.rb:23:in `measure'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/benchmarking.rb:23:in `render'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/base.rb:447:in `render_action'' app/controllers/reviews_controller.rb:91:in `create_curriculum'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/base.rb:708:in `send'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/base.rb:708:in `perform_action_without_filters'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/filters.rb:294:in `perform_action_without_benchmark'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/benchmarking.rb:37:in `perform_action_without_rescue'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/benchmarking.rb:37:in `measure'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/benchmarking.rb:37:in `perform_action_without_rescue'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/rescue.rb:79:in `perform_action'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/base.rb:330:in `send'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/base.rb:330:in `process'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.8.1/lib/action_controller/base.rb:273:in `process'' /usr/lib/ruby/gems/1.8/gems/rails-0.12.1/lib/dispatcher.rb:32:in `dispatch'' /home/prs6241/public_html/hsr/dispatch.fcgi:20 /home/prs6241/public_html/hsr/dispatch.fcgi:18:in `each_cgi'' /usr/lib/ruby/gems/1.8/gems/fcgi-0.8.5/./fcgi.rb:597:in `each'' /usr/lib/ruby/gems/1.8/gems/fcgi-0.8.5/./fcgi.rb:597:in `each_cgi'' /home/prs6241/public_html/hsr/dispatch.fcgi:18 Request Parameters: {"review"=>{"rating"=>"2", "grade"=>"all", "reviewed_by"=>"", "product_type"=>"curriculum", "product"=>"", "approved"=>"N", "reviewer_email"=>"", "used_since"=>"2000", "what_i_liked"=>"", "subject_id"=>"17", "publisher_id"=>"2", "what_could_be_changed"=>""}, "submit"=>"Save"} This error seems to generate when Rails attempts to re-render the add form. Removing the ''collection_select...'' statements cause the Rails error page to be displayed as mentioned in the Beta Book. Any suggestions on how to solve this??? -Larry
Larry Kelly
2005-Jun-13 21:45 UTC
validations not working with collection population droplists
I have an ADD form with foreign key field values selectable via select list ( collection_select (...)). When I add validates_presence_of :someotherfield to my model for error trapping, I get the following error for each droplist: NoMethodError in Reviews#create_curriculum Showing /reviews/_form.rhtml where line #16 raised: undefined method `inject'' for nil:NilClass Extracted source (around line #16): 13: <tr> 14: <td class="menutitle">Who published it?:</td> 15: <td> 16: <%= collection_select ''review'', ''publisher_id'', @publishers, ''id'', ''name'' %> 17: <%= link_to ''Add'', :controller => ''publishers'', :action => ''new'' %> 18: </td> 19: </tr> Request Parameters: {"review"=>{"rating"=>"2", "grade"=>"all", "reviewed_by"=>"", "product_type"=>"curriculum", "product"=>"", "approved"=>"N", "reviewer_email"=>"", "used_since"=>"2000", "what_i_liked"=>"", "subject_id"=>"17", "publisher_id"=>"2", "what_could_be_changed"=>""}, "submit"=>"Save"} This error seems to generate when Rails attempts to re-render the add form. Removing the ''collection_select...'' statements cause the Rails error page to be displayed as mentioned in the Beta Book. Any suggestions on how to solve this??? -Larry
Michael Schuerig
2005-Jun-13 23:29 UTC
Re: validations not working with collection population droplists
On Monday 13 June 2005 23:45, Larry Kelly wrote:> I have an ADD form with foreign key field values selectable via > select list ( collection_select (...)). > When I add validates_presence_of :someotherfield to my model for > error trapping, I get the following error for each droplist: > > NoMethodError in Reviews#create_curriculum > > Showing /reviews/_form.rhtml where line #16 raised: > > undefined method `inject'' for nil:NilClassHere''s what I think happens: The error happens when you edit a Review and validation fails for some reason. Then, from the create or update action your controller calls render_action ''edit'' which causes the error. This is because when the template for the edit action is rendered, @publishers is not initialized. render_action ''edit'' does *not* call the edit method in your controller, thus when your edit view is displayed from the create or update action, you need to take care that the required instance variables are initialized. It may be enough if you just call edit explicitly. Or do it like this def edit @review ||= Review.find(params[:id]) @publishers ||= ... ... end This ensures that on an edit request all variables are freshly initialized, but when edit is called from one of the other action methods, existing values are not clobbered. In particular, you don''t want the validation errors in @review.errors to be overwritten by a reloaded object. Michael -- Michael Schuerig The more it stays the same, mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org The less it changes! http://www.schuerig.de/michael/ --Spinal Tap, The Majesty of Rock
Larry Kelly
2005-Jun-14 04:01 UTC
Re: Re: validations not working with collection population droplists
Calling the ''edit'' and ''new'' actions explicitly AND change the action to us the: @publisher ||= .... code did the trick. THANKS! -Larry On 6/13/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Monday 13 June 2005 23:45, Larry Kelly wrote: > > I have an ADD form with foreign key field values selectable via > > select list ( collection_select (...)). > > When I add validates_presence_of :someotherfield to my model for > > error trapping, I get the following error for each droplist: > > > > NoMethodError in Reviews#create_curriculum > > > > Showing /reviews/_form.rhtml where line #16 raised: > > > > undefined method `inject'' for nil:NilClass > > Here''s what I think happens: > > The error happens when you edit a Review and validation fails for some > reason. Then, from the create or update action your controller calls > render_action ''edit'' which causes the error. This is because when the > template for the edit action is rendered, @publishers is not > initialized. > > render_action ''edit'' does *not* call the edit method in your controller, > thus when your edit view is displayed from the create or update action, > you need to take care that the required instance variables are > initialized. It may be enough if you just call edit explicitly. Or do > it like this > > def edit > @review ||= Review.find(params[:id]) > @publishers ||= ... > ... > end > > This ensures that on an edit request all variables are freshly > initialized, but when edit is called from one of the other action > methods, existing values are not clobbered. In particular, you don''t > want the validation errors in @review.errors to be overwritten by a > reloaded object. > > Michael > > -- > Michael Schuerig The more it stays the same, > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org The less it changes! > http://www.schuerig.de/michael/ --Spinal Tap, The Majesty of Rock > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >