Reputation: 2999
I'm trying to create a new course create form with a drop down to select a teach from the teacher's table.
When I put the below in my new course form view I get this error:
You have a nil object when you didn't expect it!
<%= collection_select(:Teacher, :id, @teachers, :id, :name, options = {:prompt => "Select a Teacher"}) %>
if I put
<%= collection_select(:Teacher, :id, Teacher.find(:all), :id, :name, options = {:prompt => "Select a Teacher"}) %>
it creates the form with the correct drop down info but then it won't save.
My course controller create method looks like this
def create
@course = Course.new(params[:course])
respond_to do |format|
if @course.save
format.html { redirect_to(@course, :notice => 'Course was successfully created.') }
format.xml { render :xml => @course, :status => :created, :location => @course }
else
format.html { render :action => "new" }
format.xml { render :xml => @course.errors, :status => :unprocessable_entity }
end
Upvotes: 0
Views: 571
Reputation: 8744
Rewrite new action like this
def new @course = Course.new @teachers = Teachers.all respond_to do |format| format.html # new.html.erb format.xml { render :xml => @course } end end
After that
<%= collection_select(:Teacher, :id, @teachers, :id, :name, options = {:prompt => "Select a Teacher"}) %>
should work
Upvotes: 2