Was entspricht <%= f.hidden_field :_destroy %>
für nullify anstatt zu zerstören? (Ich wollte es nur aus der Assoziation entfernen, aber ich will es nicht zerstören).Rails verschachteltes Modell - Verbindung entfernen
wäre ein Beispiel Situation:
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
In 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 %>
Eine kleine Änderung an der Lösung
def remove
#!self.foo_id.nil? should be:
false #this way newly created objects aren't destroyed, and neither are existing ones.
end
So, jetzt kann ich anrufen in .bearbeiten .html:
<%= builder.hidden_field :_remove %>
Sie, Sir, sind sehr hilfreich. – SooDesuNe