简体   繁体   English

Rails Cocoon gem没有错误,也没有输出

[英]No errors and no output for rails cocoon gem

I am working on a dynamically nested form using the cocoon gem. 我正在使用茧形宝石动态嵌套表单。 I have two models 我有两个模型

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

I have cocoon and jquery-rails in the gemfile I added //=require cocoon to the application.js file 我在我添加的gemfile中有茧和jquery-rails // =需要将茧放在application.js文件中

And here is my form partial 这是我的部分表格

<%= 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 is an instance of the cross table model. @table是交叉表模型的实例。 Nothing in the foreign_field_fields partial shows up and link_to_add_association does nothing, and I get no errors. Foreign_field_fields部分中没有任何显示,并且link_to_add_association没有任何作用,并且我也没有收到任何错误。 How can I start debugging this? 我该如何开始调试呢? Does anyone spot an error? 有人发现错误吗?

You wrote the link_to_add_association inside the simple_fields_for , which will loop over all :foreign_fields and execute the given block. 您在simple_fields_for内编写了link_to_add_association ,它将遍历所有:foreign_fields并执行给定的块。 So if there are no foreign-fields yet, the link_to_add_association is never shown. 因此,如果还没有外部字段,则永远不会显示link_to_add_association

You should write your view as follows (as documented): 您应该按如下方式编写您的视图(如所记录):

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM