这是我在这里的第一篇文章,所以试着忍受我。我试图将nested_form gem合并到我的Rails应用程序中。我有一个徽章模型,它有许多BadgeElements。我希望能够动态添加和删除BadgeElements,但是我无法让gem正常工作。我不知道它是我的设置还是我如何调用它。 (我已经遍布StackOverflow和Google寻找答案,没有任何帮助。)我已经安装了" nested_form"在我的Gemfile中添加了gem(0.3.2),添加了" // = require jquery_nested_form" line到我的application.js,并改变了我的" form_for"到" nested_form_for",但添加和删除的链接都不起作用。当我点击它们时,我得到一个" javascript:void(0)"信息。有人能告诉我我做错了什么吗?
以下是相关的代码位供参考:
badge.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:
= nested_form_for @badge, html:{class:"form-horizontal edit_badge", data:{id:@badge.id}} do |f|
%table{class: "table"}
%tbody
.form-group
= render partial: "badge_elements/form", locals: {f:f}
= f.link_to_add "Add an element", :badge_elements
badge_elements / _form.html.haml:
= f.fields_for :badge_elements do |j|
%tr
%td
%td= j.label :side
%td= j.text_field :side, id: "badge_element_#{j.object.id}_side"
...
...
%tr
%td
%td= j.link_to_remove "remove element"
我正在运行Rails 4.感谢任何帮助。提前谢谢!