4
相当于<%= f.hidden_field :_destroy %>
用于取消而不是销毁? (即我只是将它从协会中删除,但我不想摧毁它)。Rails嵌套模型 - 删除关联
一个例子的情况是:
class Foo < ActiveRecord::Base
has_many :bar, :dependent=>:nullify, :autosave=>true
accepts_nested_attributes_for :bar, :reject_if => proc { |attributes| attributes.all? {|k,v| v.blank?} }
class Bar < ActiveRecord::Base
belongs_to :foo
在Foo的edit.html.erb
:
<%= f.fields_for :bar do |builder| %>
<%= builder.some_rails_helper %>
<%= builder.hidden_field :_remove #<-- set value to 1 to destroy, but how to unassociate?%>
<% end %>
一个小的修改,以解决
def remove
#!self.foo_id.nil? should be:
false #this way newly created objects aren't destroyed, and neither are existing ones.
end
所以,现在我可以打电话。编辑html的:
<%= builder.hidden_field :_remove %>
您好,先生,非常有帮助。 – SooDesuNe 2010-12-08 01:08:29