首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套字段- RoR - link_to_add_associations

嵌套字段- RoR - link_to_add_associations
EN

Stack Overflow用户
提问于 2020-03-04 23:50:34
回答 1查看 17关注 0票数 0

我在一个带有3个表的表单中使用了cocoon和嵌套字段,如此db方案所示:)

我想添加一个新菜单,因此在a menus/new.html.erb中我有:

代码语言:javascript
复制
    <%= 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 %>

在我的partial中,嵌套字段为_portion_fields.html.erb:

代码语言:javascript
复制
<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>

和我的模型:

代码语言:javascript
复制
  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

这个错误告诉我:

EN

回答 1

Stack Overflow用户

发布于 2020-03-05 02:09:19

link_to_remove_association助手(Cocoon::ViewHelpers)从cocoon railtie加载到应用程序启动,如果您在gem安装后没有重新启动应用程序服务器,则可能会发生此错误(也要执行spring stop以确保)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60529848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档