我正在使用Rails 5和简单的表单来构建应用程序。我正在尝试让每个嵌套模型使用Twitter Bootstrap在不同的选项卡或列中显示其字段。我现在有这种情况,在创建新的或编辑/更新现有父记录时(此处为“文档类型”,嵌套模型的现有记录(此处为“关键字”)得到重复。删除使用:_destroy不起作用
我可以单独创建和删除。现在希望将其添加到父级并允许编辑。这是我将要使用的更多东西。很高兴让它发挥作用。这是如何解决的?
这些是我的模特:
class Documenttype < ApplicationRecord
has_many :annotations, dependent: :restrict_with_error
has_many :documents, dependent: :restrict_with_error
has_many :tagtypes, dependent: :restrict_with_error
has_many :keywords, dependent: :destroy
accepts_nested_attributes_for :keywords, allow_destroy: true
validates :name, presence: true, uniqueness: true, length: { minimum: 5 }
scope :active, -> { where(active: true) }
end
class Keyword < ApplicationRecord
belongs_to :documenttype
validates :keywords, presence: true
validates :language, presence: true
end
这是表格:
<%= simple_form_for @documenttype, html: { class: 'form-horizontal', multipart: true },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
boolean: :horizontal_boolean
} do |f| %>
<div class="btn-toolbar btn-group" role="toolbar">
<%= f.button :submit, :class => "btn btn-xs btn-default" %> <%= link_to 'List' , documenttypes_path, :class => "btn btn-xs btn-default" %>
</div>
<h4>Document Type</h4>
<div class="col-md-6">
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Enter name' %>
<%= f.input :description, placeholder: 'Description' %>
<%= f.input :active, as: :boolean %>
</div>
<div class="col-md-6">
<%= f.simple_fields_for :keywords do |ff| %>
<div class="panel panel-body panel-default">
<div class="col-md-3">
<%= ff.input :language, :collection => ["NL","EN"], :label => false %>
</div>
<div class="col-md-8">
<%= ff.input :keywords, placeholder: 'add keywords separated by " ; " - example: document number; document date' %>
</div>
<div class="col-md-1">
<%= ff.check_box :_destroy, label: "del"%>
</div>
</div>
<% end -%>
</div>
<% end -%>
答案 0 :(得分:0)
从这个问题中学习:
:id
之外):_destroy
也添加到strongparams accepts_nested_attributes_for :keywords, allow_destroy: true, reject_if: :something_to_check