<%= f.hidden_field :_destroy %>
破壊する代わりに無効化することと同等のものは何ですか? (つまり、関連付けから削除するだけでしたが、破棄したくありません)。
状況の例は次のとおりです。
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's で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 %>
ソリューションへの 1 つの小さな変更
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 %>