我实际上正在尝试 cocoon gem(by nathanvda)和 simple_form gem 来动态添加和删除表单上的字段。我不明白为什么嵌套表单没有出现在我的表单上。这是代码。(我'我当然是新手)。请我帮忙。
菜单/_form.html.erb
<%= simple_form_for @menu do |f| %>
<%= f.input :name %> <%= f.input :price %> <%= f.simple_fields_for :drinks do |drink| %> =render "drink_fields", :f => drink.links %> <%= link_to_add_association "Add drink"%> <%end%> <%= f.button :submit %> <%end%>
菜单/_drink_fields.html.erb
.nested-fields <%= f.input :name %> <%= link_to_remove_association "删除饮料", f %>
这些是模型
class Drinks < ActiveRecord::Base
belongs_to :menu
end
class Menu < ActiveRecord::Base
attr_accessible :drinks_attributes
has_many :drinks, :dependent => :destroy
accepts_nested_attributes_for :drinks
end
在资产/javascripts/application.js
//= require cocoon
在布局/application.html.erb
<%= javascript_include_tag :cocoon %>