rails cocoon gem没有错误也没有输出

The*_* GS 2 ruby-on-rails nested-forms simple-form cocoon-gem

我正在使用cocoon gem处理动态嵌套的表单.我有两个型号

class CrossTable < ActiveRecord::Base
  attr_accessible :title, :table_name, :database, :folder_label_id, :foreign_fields_attributes

  belongs_to :folder_label
  has_many :foreign_fields

  accepts_nested_attributes_for :foreign_fields

  validates :title, :table_name, :database, :folder_label_id, presence: true

end


class ForeignField < ActiveRecord::Base
  attr_accessible :cross_table_id, :column_name, :description

  belongs_to :cross_table
  has_many :filter_sets


end
Run Code Online (Sandbox Code Playgroud)

我在gemfile中添加了cocoon和jquery-rails //添加// = require cocoon到application.js文件

这是我的形式部分

<%= simple_form_for @table do |f| %>
    <%= f.input :title %>

    <%= f.input :folder_label_id, :collection => @folders, :label_method => :title, :value_method => :id %>
    <br><br>
    <%= f.input :table_name %>
    <%= f.input :database %>

    <%= f.simple_fields_for :foreign_fields do |fields| %>
        <%= render 'foreign_field_fields', :f => fields %>
        <div id='links'>
            <%= link_to_add_association 'Add Field', f, :foreign_fields %>
        </div>
        <% end %>

    <%= f.button :submit %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

@table是交叉表模型的一个实例.foreign_field_fields partial中没有任何内容显示,link_to_add_association什么都不做,我没有错误.我该如何开始调试呢?有人发现错误吗?

nat*_*vda 5

你在link_to_add_association里面编写了simple_fields_for,它将遍历所有:foreign_fields并执行给定的块.因此,如果还没有外国字段,link_to_add_association则永远不会显示.

您应该按如下方式编写视图(如文档所述):

<%= simple_form_for @table do |f| %>
    <%= f.input :title %>

    <%= f.input :folder_label_id, :collection => @folders, :label_method => :title, :value_method => :id %>
    <br><br>
    <%= f.input :table_name %>
    <%= f.input :database %>

    <%= f.simple_fields_for :foreign_fields do |fields| %>
        <%= render 'foreign_field_fields', :f => fields %>
    <% end %>
    <div id='links'>
      <%= link_to_add_association 'Add Field', f, :foreign_fields %>
    </div>

    <%= f.button :submit %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.