collection_select 未保存/出错
我正在尝试创建一个新的课程创建表单,其中包含下拉菜单以从教师表中选择教师。
当我将以下内容放入新课程表单视图中时,出现此错误:
当你没有预料到时,你得到了一个 nil 对象!
<%= collection_select(:Teacher, :id, @teachers, :id, :name, options = {:prompt => "Select a Teacher"}) %>
如果我把
<%= collection_select(:Teacher, :id, Teacher.find(:all), :id, :name, options = {:prompt => "Select a Teacher"}) %>
它创建带有正确下拉信息的表单,但它不会保存。
我的课程控制器创建方法如下所示
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样重写新操作
之后
应该可以工作
Rewrite new action like this
After that
should work