I have a form. Everything submits into the database if I don''t have any validation rules. When I put these rules: validates_uniqueness_of :title, :message => "already exists in database" validates_presence_of :title I get this error (which seems weird because I am not validating against that): You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.each Extracted source (around line #18): 15: <p><label for="resource_focus_id">Focus</label><br/> 16: <select name="resource[focus_id]" id="resource_focus_id"> 17: <option value="" selected="selected"></option> 18: <% for @focus in @focuses %> 19: <option value="<%= @focus.id %>"><%= @focus.name %></option> 20: <% end %> 21: </select> Seth Buntin Web Resources Coordinator Kentucky Academy of Technology Education Murray State University -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060221/dae6adf0/attachment-0001.html
On Tue, 2006-02-21 at 13:59 -0600, Buntin, Seth - KATE wrote:> I have a form. Everything submits into the database if I don?t have > any validation rules. When I put these rules: > > > > validates_uniqueness_of :title, :message => "already exists in > database" > > validates_presence_of :title > > > > I get this error (which seems weird because I am not validating > against that): > > > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.each > > > > Extracted source (around line #18): > > 15: <p><label for="resource_focus_id">Focus</label><br/> > 16: <select name="resource[focus_id]" id="resource_focus_id"> > 17: <option value="" selected="selected"></option> > 18: <% for @focus in @focuses %> > 19: <option value="<%= @focus.id %>"><%= @focus.name %></option> > 20: <% end %> > 21: </select> >---- I''m working on a very similar thing - I can''t get the validations to work (separate thread) In your case, if you add ! to your controller... if focus.save! # I''m guessing that it''s a ''focus'' you are trying to save Then you will get an active record error instead of the meaningless view code error Craig
Its because you are redisplaying your form with out reloading the options for your form. The render method does just that, it simply renders the view, it doesn''t call any other methods in your controller. Try adding this before you render the form again: @focuses = Focus.find :all On 2/21/06, Buntin, Seth - KATE <Seth.Buntin@coe.murraystate.edu> wrote:> > > > I have a form. Everything submits into the database if I don''t have any > validation rules. When I put these rules: > > > > validates_uniqueness_of :title, :message => "already exists in database" > > validates_presence_of :title > > > > I get this error (which seems weird because I am not validating against > that): > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.each > > > > > Extracted source (around line #18): 15: <p><label > for="resource_focus_id">Focus</label><br/> > 16: <select name="resource[focus_id]" id="resource_focus_id"> > 17: <option value="" selected="selected"></option> > 18: <% for @focus in @focuses %> > 19: <option value="<%= @focus.id %>"><%= @focus.name %></option> > 20: <% end %> > 21: </select> > > > > > > > Seth Buntin > > Web Resources Coordinator > > Kentucky Academy of Technology Education > > Murray State University > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
I don''t understand. I am putting in bad values so I can errors and view them. Where would I put @focuses = Focus.find :all? -----Original Message----- From: Nick Stuart [mailto:nicholas.stuart@gmail.com] Sent: Tuesday, February 21, 2006 2:23 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Validation issues Its because you are redisplaying your form with out reloading the options for your form. The render method does just that, it simply renders the view, it doesn''t call any other methods in your controller. Try adding this before you render the form again: @focuses = Focus.find :all On 2/21/06, Buntin, Seth - KATE <Seth.Buntin@coe.murraystate.edu> wrote:> > > > I have a form. Everything submits into the database if I don''t haveany> validation rules. When I put these rules: > > > > validates_uniqueness_of :title, :message => "already exists indatabase"> > validates_presence_of :title > > > > I get this error (which seems weird because I am not validatingagainst> that): > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.each > > > > > Extracted source (around line #18): 15: <p><label > for="resource_focus_id">Focus</label><br/> > 16: <select name="resource[focus_id]" id="resource_focus_id"> > 17: <option value="" selected="selected"></option> > 18: <% for @focus in @focuses %> > 19: <option value="<%= @focus.id %>"><%= @focus.name%></option>> 20: <% end %> > 21: </select> > > > > > > > Seth Buntin > > Web Resources Coordinator > > Kentucky Academy of Technology Education > > Murray State University > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Because you are redisplaying to form for the user to correct right? And in that form you have a select box for those values right? Well, you need values to select from! It has nothing to do with validations at all, but with just the way rails works. You are making a new request, the old values from your previous request aren''t saved and need to be reloaded. -Nick On 2/21/06, Buntin, Seth - KATE <Seth.Buntin@coe.murraystate.edu> wrote:> I don''t understand. I am putting in bad values so I can errors and view > them. Where would I put @focuses = Focus.find :all? > > -----Original Message----- > From: Nick Stuart [mailto:nicholas.stuart@gmail.com] > Sent: Tuesday, February 21, 2006 2:23 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Validation issues > > Its because you are redisplaying your form with out reloading the > options for your form. The render method does just that, it simply > renders the view, it doesn''t call any other methods in your > controller. > > Try adding this before you render the form again: > @focuses = Focus.find :all > > On 2/21/06, Buntin, Seth - KATE <Seth.Buntin@coe.murraystate.edu> wrote: > > > > > > > > I have a form. Everything submits into the database if I don''t have > any > > validation rules. When I put these rules: > > > > > > > > validates_uniqueness_of :title, :message => "already exists in > database" > > > > validates_presence_of :title > > > > > > > > I get this error (which seems weird because I am not validating > against > > that): > > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of Array. > > The error occured while evaluating nil.each > > > > > > > > > > Extracted source (around line #18): 15: <p><label > > for="resource_focus_id">Focus</label><br/> > > 16: <select name="resource[focus_id]" id="resource_focus_id"> > > 17: <option value="" selected="selected"></option> > > 18: <% for @focus in @focuses %> > > 19: <option value="<%= @focus.id %>"><%= @focus.name > %></option> > > 20: <% end %> > > 21: </select> > > > > > > > > > > > > > > Seth Buntin > > > > Web Resources Coordinator > > > > Kentucky Academy of Technology Education > > > > Murray State University > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
insert the line just before your else render :action => ... that causes the page to reload. Craig On Tue, 2006-02-21 at 14:50 -0600, Buntin, Seth - KATE wrote:> I don''t understand. I am putting in bad values so I can errors and view > them. Where would I put @focuses = Focus.find :all? > > -----Original Message----- > From: Nick Stuart [mailto:nicholas.stuart@gmail.com] > Sent: Tuesday, February 21, 2006 2:23 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Validation issues > > Its because you are redisplaying your form with out reloading the > options for your form. The render method does just that, it simply > renders the view, it doesn''t call any other methods in your > controller. > > Try adding this before you render the form again: > @focuses = Focus.find :all > > On 2/21/06, Buntin, Seth - KATE <Seth.Buntin@coe.murraystate.edu> wrote: > > > > > > > > I have a form. Everything submits into the database if I don''t have > any > > validation rules. When I put these rules: > > > > > > > > validates_uniqueness_of :title, :message => "already exists in > database" > > > > validates_presence_of :title > > > > > > > > I get this error (which seems weird because I am not validating > against > > that): > > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of Array. > > The error occured while evaluating nil.each > > > > > > > > > > Extracted source (around line #18): 15: <p><label > > for="resource_focus_id">Focus</label><br/> > > 16: <select name="resource[focus_id]" id="resource_focus_id"> > > 17: <option value="" selected="selected"></option> > > 18: <% for @focus in @focuses %> > > 19: <option value="<%= @focus.id %>"><%= @focus.name > %></option> > > 20: <% end %> > > 21: </select> > > > > > > > > > > > > > > Seth Buntin > > > > Web Resources Coordinator > > > > Kentucky Academy of Technology Education > > > > Murray State University > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Yeah, I was kinda late with that one...sorry guys for the wasted time. Now I am having other problems. A couple of the collection_selects display other selections (via AJAX). The "other selections" are blank when the page is rendered to display the errors. How do I take care of this? -----Original Message----- From: Craig White [mailto:craigwhite@azapple.com] Sent: Tuesday, February 21, 2006 2:59 PM To: rails@lists.rubyonrails.org Subject: RE: [Rails] Validation issues insert the line just before your else render :action => ... that causes the page to reload. Craig On Tue, 2006-02-21 at 14:50 -0600, Buntin, Seth - KATE wrote:> I don''t understand. I am putting in bad values so I can errors andview> them. Where would I put @focuses = Focus.find :all? > > -----Original Message----- > From: Nick Stuart [mailto:nicholas.stuart@gmail.com] > Sent: Tuesday, February 21, 2006 2:23 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Validation issues > > Its because you are redisplaying your form with out reloading the > options for your form. The render method does just that, it simply > renders the view, it doesn''t call any other methods in your > controller. > > Try adding this before you render the form again: > @focuses = Focus.find :all > > On 2/21/06, Buntin, Seth - KATE <Seth.Buntin@coe.murraystate.edu>wrote:> > > > > > > > I have a form. Everything submits into the database if I don''t have > any > > validation rules. When I put these rules: > > > > > > > > validates_uniqueness_of :title, :message => "already exists in > database" > > > > validates_presence_of :title > > > > > > > > I get this error (which seems weird because I am not validating > against > > that): > > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of Array. > > The error occured while evaluating nil.each > > > > > > > > > > Extracted source (around line #18): 15: <p><label > > for="resource_focus_id">Focus</label><br/> > > 16: <select name="resource[focus_id]" id="resource_focus_id"> > > 17: <option value="" selected="selected"></option> > > 18: <% for @focus in @focuses %> > > 19: <option value="<%= @focus.id %>"><%= @focus.name > %></option> > > 20: <% end %> > > 21: </select> > > > > > > > > > > > > > > Seth Buntin > > > > Web Resources Coordinator > > > > Kentucky Academy of Technology Education > > > > Murray State University > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Buntin, Seth - KATE wrote:> I have a form. Everything submits into the database if I don?t have any > validation rules. When I put these rules: > > > > validates_uniqueness_of :title, :message => "already exists in database" > > validates_presence_of :title > > > > I get this error (which seems weird because I am not validating against > that): > > > > |You have a nil object when you didn''t expect it!| > > |You might have expected an instance of Array.| > > |The error occured while evaluating nil.each| > > > > Extracted source (around line *#18*): > > |15: <p><label for="resource_focus_id">Focus</label><br/>| > |16: <select name="resource[focus_id]" id="resource_focus_id">| > |17: <option value="" selected="selected"></option>| > |18: <% for @focus in @focuses %>| > |19: <option value="<%= @focus.id %>"><%= @focus.name %></option>| > |20: <% end %>| > |21: </select>| >You should probably confirm what is in focuses with something like <!-- <%= forcuses.inspect() %> --> I think you will find focuses is a ''nil'' I think this should be using a local variable <% for focus in @focuses %>| <option value="<%= focus.id %>"><%= focus.name %></option>| <% end %>|> > > > > Seth Buntin > > Web Resources Coordinator > > Kentucky Academy of Technology Education > > Murray State University > >Regards Neil.