Rails Cocoon gem没有错误,也没有输出 [英] No errors and no output for rails cocoon gem

查看:95
本文介绍了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

我在gemfile中有茧和jquery-rails 我在application.js文件中添加了//=需要茧

I have cocoon and jquery-rails in the gemfile I added //=require cocoon to the application.js file

这是我的部分表格

<%= 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不执行任何操作,并且我也没有收到任何错误.我该如何开始调试呢?有人发现错误了吗?

@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?

推荐答案

您在simple_fields_for内编写了link_to_add_association,它将循环遍历所有:foreign_fields并执行给定的块.因此,如果还没有异域,则永远不会显示link_to_add_association.

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

希望这会有所帮助.

这篇关于Rails Cocoon gem没有错误,也没有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆