3

我正在使用 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

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

这是我的部分表格

<%= 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 %>

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

4

1 回答 1

6

你写了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 %>

希望这可以帮助。

于 2012-10-24T22:17:46.537 回答