【问题标题】:accept_nested_attributes is not destroying the recordaccept_nested_attributes 没有破坏记录
【发布时间】:2013-03-04 10:05:22
【问题描述】:

我的问题与 accept_nested_attributes 相关,我有一个型号名称 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

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

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"}}

请澄清我做错了什么。

【问题讨论】:

  • 你可以发布查看代码吗?
  • 我已经在我的问题中发帖了

标签: ruby-on-rails ruby-on-rails-3 nested-attributes has-many fields-for


【解决方案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

【讨论】:

  • 已添加到我的模型中,但问题仍然存在
  • 我知道这是一个愚蠢的问题,但是您是否重新启动了服务器?您的日志看起来不错,表单正在发送正确的参数,除非您的控制器中有一些非常奇怪的东西,否则这似乎是一个质量属性问题。
  • 那么解决方案是什么?回答你自己的问题,所以它在stackoverflow中,以防其他人遇到同样的问题并且它与质量属性无关
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 2013-09-15
  • 1970-01-01
  • 2017-10-30
  • 2011-09-19
相关资源
最近更新 更多