问题
I have three models:
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :profile
belongs_to :theme
has_many :linked_sources
attr_accessible :body, :theme_id, :question_id, :linked_sources_attributes
validates_presence_of :body, :profile, :question, :theme
has_paper_trail :on => [:update]
accepts_nested_attributes_for :linked_sources, reject_if: :all_blank
end
class Answer::LinkedSource < ActiveRecord::Base
belongs_to :answer
belongs_to :source
validates :source, :description, presence: true
validates_presence_of :answer_id, :unless => :nested
attr_accessor :nested
accepts_nested_attributes_for :source, reject_if: :all_blank, allow_destroy: true
end
class Source < ActiveRecord::Base
SOURCE_TYPES = %w(book film)
has_many :linked_sources, class_name: 'Answer::LinkedSource'
has_many :answers, through: :linked_sources
validates :source_type, inclusion: {in: SOURCE_TYPES}
validates :source_type, :title, presence: true
end
I have a form with two nested already existing Linked Sources + Sources for already existing Answer. In my _linked_source_fields partial I have link_to_remove_association and it works properly, setting the value of "_destroy" input to "1".
When I remove two resources and press submit button, I get the following form data sending out:
utf8:✓
_method:put
authenticity_token:726c1e7NIb0Je2uUZYeKLXmqgFHxgakfcF6fzpjFb38=
answer[theme_id]:2
answer[body]:retert
_wysihtml5_mode:1
answer[question_id]:22
answer[linked_sources_attributes][0][source_id]:3
answer[linked_sources_attributes][0][nested]:
answer[linked_sources_attributes][0][source_attributes][source_type]:book
answer[linked_sources_attributes][0][source_attributes][title]:erger
answer[linked_sources_attributes][0][source_attributes][id]:3
answer[linked_sources_attributes][0][description]:erger ter
answer[linked_sources_attributes][0][_destroy]:1
answer[linked_sources_attributes][0][id]:3
answer[linked_sources_attributes][1][source_id]:4
answer[linked_sources_attributes][1][nested]:
answer[linked_sources_attributes][1][source_attributes][source_type]:film
answer[linked_sources_attributes][1][source_attributes][title]:terter
answer[linked_sources_attributes][1][source_attributes][id]:4
answer[linked_sources_attributes][1][description]:retr
answer[linked_sources_attributes][1][_destroy]:1
answer[linked_sources_attributes][1][id]:4
commit:Готово
And that seems to be correct data.
However after saving this form both linked_sources are still present. The server side just ignores "_destroy" parameters.
What's wrong? I have another cocoon form for another models with simplier nesting (just one level) in this project and it works fine, but in the other case it doesn't.
(As you can notice, my English isn't perfect — sorry about that — it would be fine if you'll correct it)
回答1:
You must add allow_destroy: true
to the association's accepts_nested_attributes_for
:
accepts_nested_attributes_for :linked_sources,
reject_if: :all_blank,
allow_destroy: true
See the docs for more details.
来源:https://stackoverflow.com/questions/10621013/cannot-destroy-existing-nested-association