我正在以具有3个表的形式使用茧和嵌套字段,如此db方案所示:)我想添加一个新菜单,所以在menus / new.html.erb中,我有:
<%= simple_form_for @menu do |f| %>
<%= f.input :name, label: "name", label_method: :name, value_method: :id, include_blank: true %>
<h3>Portions</h3>
<div id='portions'>
<%= f.simple_fields_for :portions do |portion| %>
<%= render 'portion_fields', :f => portion %>
<% end %>
<div class='links'>
<%= link_to_add_association 'add portion', f, :portions %>
</div>
</div>
<%= f.submit 'valider' %>
<% end %>
在我的部分中为_portion_fields.html.erb的嵌套字段:
<div class='nested-fields'>
<%= f.association :aliment, label: "Intitulé de l'aliment", label_method: :label, value_method: :id, include_blank: true %>
<%= f.input :portion_qty, label: "Qté", placeholder:"portions", as: :integer %>
<%= link_to_remove_association "remove portion", f %>
</div>
和我的模特:
class Menu < ApplicationRecord
has_many :portions, dependent: :destroy
has_many :aliments, through: :portions, dependent: :destroy
accepts_nested_attributes_for :portions, reject_if: :all_blank, allow_destroy: true
end
class Aliment < ApplicationRecord
has_many :portions
has_many :menus, through: :portions, dependent: :destroy
end
class Portion < ApplicationRecord
belongs_to :aliment, foreign_key: :aliment_id
belongs_to :menu, foreign_key: :menu_id
end
错误告诉我:
[link_to_remove_association
助手(Cocoon::ViewHelpers
)”是从cocoon railtie加载到应用程序启动的,如果您在安装gem后没有重新启动应用程序服务器,则可能会发生此错误(请确保也执行spring stop
)。