我的问题是有关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"}}
请说清楚我做错了什么。
您可以发布视图代码? – 2013-03-04 10:11:57
我已经发布在我的问题 – 2013-03-04 10:37:10