I am trying to validate the uniqueness of a field: class Department < ActiveRecord::Base belongs_to :company belongs_to :region has_many :employees validates_uniqueness_of :dept_name end When I run it, I get this error message: NoMethodError in Department#create Showing app/views/department/_form.rhtml where line #34 raised: 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 #34): 31: <% if @page_title == "New Department" %> 32: <select name="department[company_id]"> 33: <option value="0">Select One</option> 34: <% @companies.each do |company| %> 35: <option value="<%= company.id %>"> 36: <%= company.comp_name %> 37: </option> However, if I take validates_uniqueness_of off, i.e.: class Department < ActiveRecord::Base belongs_to :company belongs_to :region has_many :employees #validates_uniqueness_of :dept_name end everything works just fine. What am I doing wrong? George --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Sep-24 20:59 UTC
Re: Validates causes NoMethodError
Hi -- On Sun, 24 Sep 2006, simplydope wrote:> > I am trying to validate the uniqueness of a field: > > class Department < ActiveRecord::Base > belongs_to :company > belongs_to :region > has_many :employees > validates_uniqueness_of :dept_name > end > > When I run it, I get this error message: > > NoMethodError in Department#create > > Showing app/views/department/_form.rhtml where line #34 raised: > > 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 #34): > > 31: <% if @page_title == "New Department" %> > 32: <select name="department[company_id]"> > 33: <option value="0">Select One</option> > 34: <% @companies.each do |company| %> > 35: <option value="<%= company.id %>"> > 36: <%= company.comp_name %> > 37: </option> > > However, if I take validates_uniqueness_of off, i.e.: > > class Department < ActiveRecord::Base > belongs_to :company > belongs_to :region > has_many :employees > #validates_uniqueness_of :dept_name > end > > everything works just fine. What am I doing wrong?I don''t know, but I have a couple of follow-up questions/suggestions: Is there anything in the stack trace that might help (including anything indicating a problem with validation itself)? Try doing stuff in the application console. That might be a good way to walk through some collection-finding mini-scenarios. If it remains a problem, post your Company model code and the view for that action. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
unknown wrote:> Hi -- > > On Sun, 24 Sep 2006, simplydope wrote: > >> When I run it, I get this error message: >> >> class Department < ActiveRecord::Base >> belongs_to :company >> belongs_to :region >> has_many :employees >> #validates_uniqueness_of :dept_name >> end >> >> everything works just fine. What am I doing wrong? > > I don''t know, but I have a couple of follow-up questions/suggestions: > > Is there anything in the stack trace that might help (including > anything indicating a problem with validation itself)? > > Try doing stuff in the application console. That might be a good way > to walk through some collection-finding mini-scenarios. > > If it remains a problem, post your Company model code and the view for > that action. > > > David > > -- > David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.orgNot exactly sure what I can "do" in the colsole. Pretty new to RoR. But here are model and view: --------- class Company < ActiveRecord::Base has_many :departments has_many :employees validates_presence_of :comp_name validates_uniqueness_of :comp_name end ---------- new.html (department) <% @page_title = "New Department" %> <%= start_form_tag :action => ''create'' %> <%= render :partial => ''form'' %> <%= end_form_tag %> ------- _form.rhtml (department) <%= error_messages_for ''department'' %> <!--[form:department]--> <table class="datainfo" width="400px"> <tr> <td colspan="2"></td> </tr> <tr> <td colspan="2"><div class="grouptitle"><%=@page_title %> </div></td> </tr> <tr> <td colspan="2"></td> </tr> <tr> <td colspan="2"><input type="submit" value="Save"/></td> </tr> <tr> <td>Dept. #:</td> <td> <%= text_field ''department'', ''dept_num'' %> <input id="deleted_yn" name="department[deleted_yn]" type="hidden" value="0" /> </td> </tr> <tr> <td>Dept. Name:</td> <td><%= text_field ''department'', ''dept_name'' %></td> </tr> <tr> <td>Company:</td> <td> <% if @page_title == "New Department" %> <select name="department[company_id]"> <option value="0">Select One</option> <% @companies.each do |company| %> <option value="<%= company.id %>"> <%= company.comp_name %> </option> <% end %> </select> <% else %> <select name="department[company_id]"> <% @companies.each do |company| %> <option value="<%= company.id %>" <%= '' selected'' if company.id == @department.company_id %>> <%= company.comp_name %> </option> <% end %> </select> <% end %> </td> </tr> <tr> <td>Region:</td> <td> <% if @page_title == "Edit Department" %> <select name="department[region_id]"> <% @regions.each do |region| %> <option value="<%= region.id %>" <%= '' selected'' if region.id == @department.region_id %>> <%= region.region %> </option> <% end %> </select> <% else %> <select name="department[region_id]"> <% @regions.each do |region| %> <option value="<%= region.id %>"> <%= region.region %> </option> <% end %> </select> <% end %> </td> </tr> <tr> <td>Address 1:</td> <td><%= text_field ''department'', ''address1'' %></td> </tr> <tr> <td>Address 2:</td> <td><%= text_field ''department'', ''address2'' %></td> </tr> <tr> <td>City:</td> <td><%= text_field ''department'', ''city'' %></td> </tr> <tr> <td>State:</td> <td><%= text_field ''department'', ''state'' %></td> </tr> <tr> <td>Zip Code:</td> <td><%= text_field ''department'', ''zip_code'' %></td> </tr> <tr> <td></td> <td><input type="checkbox" name="department[front_line_yn]" value="1">Frontline</td> </tr> <tr> <td colspan="2"></td> </tr> <tr> <td colspan="2"> <%= link_to ''Back'', :action => ''list'' %></td> </tr> </table> <!--[eoform:department]--> -------- Thanks for the help George -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 24 Sep 2006, at 21:13, simplydope wrote:> I am trying to validate the uniqueness of a field: > > class Department < ActiveRecord::Base > belongs_to :company > belongs_to :region > has_many :employees > validates_uniqueness_of :dept_name > end > > When I run it, I get this error message: > > NoMethodError in Department#create > > Showing app/views/department/_form.rhtml where line #34 raised: > > 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 > ... > 34: <% @companies.each do |company| %>Seems to me that you don''t have an @companies instance variable in whichever view includes _form. So is there a path in your department controller that can use the _form, without first initialising @companies? Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Paul Lynch wrote:> On 24 Sep 2006, at 21:13, simplydope wrote: > >> >> NoMethodError in Department#create >> >> Showing app/views/department/_form.rhtml where line #34 raised: >> >> 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 >> ... >> 34: <% @companies.each do |company| %> > > Seems to me that you don''t have an @companies instance variable in > whichever view includes _form. So is there a path in your department > controller that can use the _form, without first initialising > @companies? > > Paul----------------------------- No exactly sure what you''re asking...but here is my department_controller.rb class DepartmentController < ApplicationController def index list render :action => ''list'' end # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list } def list @department_pages, @departments = paginate :departments, :per_page => 20, :conditions => "deleted_yn = 0", :order_by => "dept_name" end def show @department = Department.find(params[:id]) end def new @department = Department.new @companies = Company.find(:all, :conditions => "deleted_yn = 0", :order => "comp_name") @regions = Region.find(:all, :conditions => "deleted_yn = 0", :order => "region") end def create @department = Department.new(params[:department]) if @department.save flash[:notice] = ''Department was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end def edit @department = Department.find(params[:id]) @companies = Company.find_all @regions = Region.find_all end def update @department = Department.find(params[:id]) if @department.update_attributes(params[:department]) flash[:notice] = ''Department was successfully updated.'' redirect_to :action => ''list'' else render :action => ''edit'' end end def destroy @department = Department.find(params[:id]) Department.update(params[:id], {:deleted_yn => 1}) redirect_to :action => ''list'' end end Thanks, George -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 25 Sep 2006, at 00:23, George wrote:> Paul Lynch wrote: >> On 24 Sep 2006, at 21:13, simplydope wrote: >> >>> NoMethodError in Department#createNote that the error is in create.>>> Showing app/views/department/_form.rhtml where line #34 raised: >>> >>> 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 >>> ... >>> 34: <% @companies.each do |company| %> >> >> Seems to me that you don''t have an @companies instance variable in >> whichever view includes _form. So is there a path in your department >> controller that can use the _form, without first initialising >> @companies?> No exactly sure what you''re asking...but here is my > department_controller.rb > > class DepartmentController < ApplicationController > ... > def new > @department = Department.new > @companies = Company.find(:all, :conditions => "deleted_yn = 0", > :order => "comp_name") > @regions = Region.find(:all, :conditions => "deleted_yn = > 0", :order > => "region") > endnew initialises @companies.> def create > @department = Department.new(params[:department]) > if @department.save > flash[:notice] = ''Department was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > endBut create doesn''t. The thought about model associations causing the problem is a red herring; copy your ''@companies = '' line into create from new, and it should work. Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Paul Lynch wrote:> On 25 Sep 2006, at 00:23, George wrote: > >> Paul Lynch wrote: >>> On 24 Sep 2006, at 21:13, simplydope wrote: >>> >>>> NoMethodError in Department#create > > Note that the error is in create. > >>> controller that can use the _form, without first initialising >>> @companies? > >> 0", :order >> => "region") >> end > > new initialises @companies. > >> def create >> @department = Department.new(params[:department]) >> if @department.save >> flash[:notice] = ''Department was successfully created.'' >> redirect_to :action => ''list'' >> else >> render :action => ''new'' >> end >> end > > But create doesn''t. > > The thought about model associations causing the problem is a red > herring; copy your ''@companies = '' line into create from new, and it > should work. > > PaulThanks. It worked. Here''s the result, for those that have this problem in the future. def new @department = Department.new @companies = Company.find(:all, :conditions => "deleted_yn = 0", :order => "comp_name") @regions = Region.find(:all, :conditions => "deleted_yn = 0", :order => "region") end def create @department = Department.new(params[:department]) @companies = Company.find(:all, :conditions => "deleted_yn = 0", :order => "comp_name") @regions = Region.find(:all, :conditions => "deleted_yn = 0", :order => "region") # I could use this for deleted_yn # @department.deleted_yn = 0 if @department.save flash[:notice] = ''Department was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---