我正在开发一个带有许多 BadgeElements 的徽章模型的 Rails 应用程序。我正在尝试允许动态添加和删除 BadgeElements,并且当我遇到 ryanb 的nested_form gem 问题时,我现在正在查看 Cocoon。我相信我已经正确安装了 gem,但是事件没有触发。当我单击用于添加或删除关联的链接时,页面顶部的 URL 会附加一个 #,否则什么也不会发生。以下是相关代码:
徽章.rb:
class Badge < ActiveRecord::Base
has_one :background, :dependent => :destroy
has_many :badge_elements, :dependent => :destroy
accepts_nested_attributes_for :background
accepts_nested_attributes_for :badge_elements, :allow_destroy => true
validates :title, presence: true, length:{minimum: 3}
end
_form.html.haml:
= form_for @badge, html:{class:"form-horizontal edit_badge", data:{id:@badge.id}} do |f|
%table
%tbody
.field
%tr
%td= f.label :title
%td= f.text_field :title
#badge_elements
= f.fields_for :badge_elements do |badge_element|
= render 'badge_element_fields', :f => badge_element
.links
= link_to_add_association 'Add an element', f, :badge_elements
_badge_element_fields.html.haml:
.nested-fields
.field
%tr
%td
%td= f.label :side
%td= f.text_field :side, id: "badge_element_#{f.object.id}_side"
...
...
= link_to_remove_association "remove element", f