1

我的问题是有关accept_nested_attributes,我有一个型号名称StudentProfile,它包含以下代码:accept_nested_attributes不破坏记录

class StudentProfile < ActiveRecord::Base 
    attr_accessible :projects_attributes 
    has_many :projects ,:inverse_of => :student_profile,:dependent => :destroy 
    accepts_nested_attributes_for :projects, :allow_destroy => true, :reject_if => lambda { |a| a[:name].blank? } 
end 

我的另一个模型包含下面的代码:

class Project < ActiveRecord::Base 
    belongs_to :student_profile 
end 

和我的视图文件包含以下代码:

<%= f.fields_for :projects do |builder| %> 
    <%= render "projects_fields", :f => builder %> 
<% end %> 
<%= link_to_add_fields "Add Project", f, :projects %> 

现在的问题是,每当我保存一份学生档案时,我实际上可以保存项目的记录,但每当我尝试更新学生档案并删除其中一个项目时,实际上并没有销毁更新项目,但我的params包括以下内容:

"projects_attributes"=>{"0"=>{"name"=>"test", "_destroy"=>"1", "id"=>"2"}} 

请说清楚我做错了什么。

+0

您可以发布视图代码? – 2013-03-04 10:11:57

+0

我已经发布在我的问题 – 2013-03-04 10:37:10

回答

1

它可能是一个质量属性的保护,在您StudentProfile,添加以下内容:

class StudentProfile < ActiveRecord::Base 
    attr_accessible :projects_attributes 

    has_many :projects ,:inverse_of => :student_profile,:dependent => :destroy 
    accepts_nested_attributes_for :projects, :allow_destroy => true, :reject_if => lambda { |a| a[:name].blank? } 
end 
+0

在我的模型中添加,但问题仍然存在 – 2013-03-04 10:21:38

+0

我知道它的一个愚蠢的问题,但你重新启动服务器?你的日志看起来很好,表单正在发送正确的参数,除非在你的控制器中有一些奇怪的东西,这似乎是一个大规模属性问题。 – rorra 2013-03-04 10:27:17

+0

是的,我做了这么多次,但没有得到成功 – 2013-03-04 10:36:29