No errors and no output for rails cocoon gem

痞子三分冷 提交于 2020-01-11 10:31:08

问题


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

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. Nothing in the foreign_field_fields partial shows up and link_to_add_association does nothing, and I get no errors. How can I start debugging this? Does anyone spot an error?


回答1:


You wrote the link_to_add_association inside the simple_fields_for, which will loop over all :foreign_fields and execute the given block. So if there are no foreign-fields yet, the link_to_add_association is never shown.

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.



来源:https://stackoverflow.com/questions/11813161/no-errors-and-no-output-for-rails-cocoon-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!