下面是添加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"}}
评论
fuliang 2007-05-01
后来我还是手工建立关联,才成功
改成:
 @student.klass_id = klass.id
 if @student.save

以前我用create也都是好使的,估计我的哪个地方还是有问题的。暂时先手工建立关联吧。
liusong1111 2007-05-01
我在testcase中跑了下,直接调用students.create正常啊,太奇怪了。
fuliang
搜索本博客
我的相册
53569b0e-134e-31fa-9555-bdfa6932b0e7-thumb
RSS Reader1
共 6 张
存档
最新评论