下面是添加student的代码
def add
@student = Student.new(params[:student])
@klass = Klass.find(:first, :conditions => [ "klass_num = ?" , params[:klass][:klass_num] ])
if @klass.students.create(@student.attributes)
flash[:notice] = "Student was successfully created"
redirect_to :action => :list
else
flash[:notice] = "Failed to create student!"
redirect_to :action => :add_student
end
end
@klass.students.create(@student.attributes)
这句本来不应该是把@student关联到@klass对象,并且保存新建的对象@student么?
但是结果并没有保存,弄了半天也没找出什么原因。
我把
if @klass.students.create(@student.attributes)
我改成:
@klass.students.build(@student.attributes)
if @student.save
保存是成功了,但是没有建立klass与student之间的关联,不知道到底哪儿有问题,希望大家指点一下
log中传递的参数是正确的:
Parameters: {"klass"=>{"klass_num"=>"210309"}, "commit"=>"添加", "action"=>"add", "controller"=>"student", "student"=>{"name"=>"fuliang", "password_confirmation"=>"123456", "student_num"=>"21030922", "password"=>"123456"}}