jhagemeier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-22 20:52 UTC
collection_select validation problem
I''m trying to assign a parent foreign key value using collection_select from my child "new" form. The problem I''m having is if I do not make a selection, I get the following error instead of the Rails validates_presence_of error: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.map If I make a selection, everything works just fine. What do I need to do to get the Rails validationvalidates_presence_of error? Here''s the code: MODEL class Client < ActiveRecord::Base has_many :programs validates_presence_of :eid, :name end class Program < ActiveRecord::Base belongs_to :client validates_presence_of :client_id validates_associated :client end CONTROLLERS def new @program = Program.new @clients = Client.find(:all, :order=>"name") respond_to do |format| format.html # new.html.erb format.xml { render :xml => @program } end end def create @program = Program.new(params[:program]) respond_to do |format| if @program.save flash[:notice] = ''Program was successfully created.'' format.html { redirect_to(@program) } format.xml { render :xml => @program, :status => :created, :location => @program } else format.html { render :action => "new" } format.xml { render :xml => @program.errors, :status => :unprocessable_entity } end end end VIIEW <% collection_select(''program'', ''client_id'', @clients, :id, :name, { :include_blank => true } ) %> Thx for the assistance! --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
jhagemeier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-23 19:53 UTC
Re: collection_select validation problem
Still stumped on this. If I replace the collection_select with a text_field, the validates_presence_of :client_id fires when I attempt to save the Program without a client_id. BUT when I use collection_select, and do not make a selection before attempting to to save the Program, I get the the nil object error. Why doesn''t the validate_presence_of: client_id catch this?? I can see in the request that the client_id is an empty. Shouldn''t that cause validates_precence_of to fail? Can anyone explain to me what I''m doing wrong here? Thanks, Jeff {"commit"=>"Create", "authenticity_token"=>"4bec56fa77ee992dc0e3e13d169bf310ee7351ff", "program"=>{"eid"=>"", "name"=>"", "end_dt(1i)"=>"2007", "end_dt(2i)"=>"12", "end_dt(3i)"=>"23", "client_id"=>"", "url"=>"", "start_dt(1i)"=>"2007", "end_dt(4i)"=>"13", "start_dt(2i)"=>"12", "end_dt(5i)"=>"45", "start_dt(3i)"=>"23", "start_dt(4i)"=>"13", "start_dt(5i)"=>"45"}} On Dec 22, 2:52 pm, "jhageme...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <jhageme...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to assign a parent foreign key value using > collection_select from my child "new" form. The problem I''m having is > if I do not make a selection, I get the following error instead of the > Rails validates_presence_of error: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.map > > If I make a selection, everything works just fine. What do I need to > do to get the Rails validationvalidates_presence_of error? > > Here''s the code: > > MODEL > > class Client < ActiveRecord::Base > has_many :programs > validates_presence_of :eid, :name > end > > class Program < ActiveRecord::Base > belongs_to :client > validates_presence_of :client_id > validates_associated :client > end > > CONTROLLERS > > def new > @program = Program.new > @clients = Client.find(:all, :order=>"name") > > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @program } > end > end > > def create > @program = Program.new(params[:program]) > > respond_to do |format| > if @program.save > flash[:notice] = ''Program was successfully created.'' > format.html { redirect_to(@program) } > format.xml { render :xml => @program, :status > => :created, :location => @program } > else > format.html { render :action => "new" } > format.xml { render :xml => @program.errors, :status > => :unprocessable_entity } > end > end > end > > VIIEW > > <%> collection_select(''program'', > ''client_id'', > @clients, > :id, > :name, > { :include_blank => true } > ) > %> > > Thx for the assistance!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Dec 2007, at 20:52, jhagemeier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > I''m trying to assign a parent foreign key value using > collection_select from my child "new" form. The problem I''m having is > if I do not make a selection, I get the following error instead of the > Rails validates_presence_of error: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.map > > If I make a selection, everything works just fine. What do I need to > do to get the Rails validationvalidates_presence_of error?Well if you encounter a validation error and render the ''new'' template. That template requires a @clients instance variable that you are setting up in the new action but not in the create action. Fred> > > Here''s the code: > > MODEL > > class Client < ActiveRecord::Base > has_many :programs > validates_presence_of :eid, :name > end > > class Program < ActiveRecord::Base > belongs_to :client > validates_presence_of :client_id > validates_associated :client > end > > CONTROLLERS > > def new > @program = Program.new > @clients = Client.find(:all, :order=>"name") > > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @program } > end > end > > def create > @program = Program.new(params[:program]) > > respond_to do |format| > if @program.save > flash[:notice] = ''Program was successfully created.'' > format.html { redirect_to(@program) } > format.xml { render :xml => @program, :status > => :created, :location => @program } > else > format.html { render :action => "new" } > format.xml { render :xml => @program.errors, :status > => :unprocessable_entity } > end > end > end > > VIIEW > > <%> collection_select(''program'', > ''client_id'', > @clients, > :id, > :name, > { :include_blank => true } > ) > %> > > Thx for the assistance! > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
jhagemeier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-23 22:39 UTC
Re: collection_select validation problem
Thank you Fred! Problem solved by adding @clients = Client.find(:all, :order=>"name") to my Create method. I was not considering that errors from Create needed to be redisplayed and collection_select is part of that form that is redisplayed and it expects a @clients instance variable. Appreciate the help. Jeff On Dec 23, 2:21 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 Dec 2007, at 20:52, jhageme...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > > I''m trying to assign a parent foreign key value using > > collection_select from my child "new" form. The problem I''m having is > > if I do not make a selection, I get the following error instead of the > > Rails validates_presence_of error: > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of Array. > > The error occurred while evaluating nil.map > > > If I make a selection, everything works just fine. What do I need to > > do to get the Rails validationvalidates_presence_of error? > > Well if you encounter a validation error and render the ''new'' > template. That template requires a @clients instance variable that you > are setting up in the new action but not in the create action. > > Fred > > > > > > > Here''s the code: > > > MODEL > > > class Client < ActiveRecord::Base > > has_many :programs > > validates_presence_of :eid, :name > > end > > > class Program < ActiveRecord::Base > > belongs_to :client > > validates_presence_of :client_id > > validates_associated :client > > end > > > CONTROLLERS > > > def new > > @program = Program.new > > @clients = Client.find(:all, :order=>"name") > > > respond_to do |format| > > format.html # new.html.erb > > format.xml { render :xml => @program } > > end > > end > > > def create > > @program = Program.new(params[:program]) > > > respond_to do |format| > > if @program.save > > flash[:notice] = ''Program was successfully created.'' > > format.html { redirect_to(@program) } > > format.xml { render :xml => @program, :status > > => :created, :location => @program } > > else > > format.html { render :action => "new" } > > format.xml { render :xml => @program.errors, :status > > => :unprocessable_entity } > > end > > end > > end > > > VIIEW > > > <%> > collection_select(''program'', > > ''client_id'', > > @clients, > > :id, > > :name, > > { :include_blank => true } > > ) > > %> > > > Thx for the assistance!- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---