什么相当于<%= f.hidden_field :_destroy %>
for nullify 而不是破坏?(即我只是要将它从协会中删除,但我不想破坏它)。
一个示例情况是:
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
所以现在我可以调用 .edit.html:
<%= builder.hidden_field :_remove %>