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!

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"}}

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

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