Javascript cocoon gem是否需要在控制器新动作中构建n个模型?

Javascript cocoon gem是否需要在控制器新动作中构建n个模型?,javascript,jquery,ruby-on-rails,ruby,apache-cocoon,Javascript,Jquery,Ruby On Rails,Ruby,Apache Cocoon,我已在rails 4应用程序中完全按照所述安装了。这在父模型表单中非常有效,允许用户添加/删除子模型的字段。我遇到的问题是提交子对象。如果子模型是在父模型的新操作中构建的,那么无论创建了多少个模型,我都可以提交,不再提交了。这在提交的参数中是显而易见的,因为它们包含子_属性(如果控制器中没有构建子模型,则不包含子属性) 当前运行 轨道4.2.10 ruby 2.5.1 茧1.2.14 jquery rails 4.3.3 jQueryUIRails 6.0.1 代码片段 class Events

我已在rails 4应用程序中完全按照所述安装了。这在父模型表单中非常有效,允许用户添加/删除子模型的字段。我遇到的问题是提交子对象。如果子模型是在父模型的新操作中构建的,那么无论创建了多少个模型,我都可以提交,不再提交了。这在提交的参数中是显而易见的,因为它们包含子_属性(如果控制器中没有构建子模型,则不包含子属性)

当前运行

轨道4.2.10

ruby 2.5.1

茧1.2.14

jquery rails 4.3.3

jQueryUIRails 6.0.1

代码片段

class EventsController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create]

  def new
    @event = Event.new
    @event.competitions.build
  end

  def create
    @event = current_user.events.create(event_params)
    if @event.valid?
      flash[:notice] = "Event created"
      redirect_to events_path
    else
      flash[:alert] = "Event not created.  Please check for errors in the form and try again."
      render :new, status: :unprocessable_entity
    end
  end

def event_params
    params.require(:event).permit(
                                  :event_name,
                                  :event_start,
                                  :event_end,
                                  :event_address,
                                  competitions_attributes: [:id, :competition_name, :maximum_participants, :type_id, :fee, :_destroy]
    )
  end
end
子模型

class Competition < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :event
  has_many :participants, dependent: :destroy
  belongs_to :type
  accepts_nested_attributes_for :participants, allow_destroy: true  

从Rails控制台(插入父模型后)


我已经经历了gem设置的所有常见问题(接受嵌套属性、反向属性、子字段的命名和缩进、安装jQuery和调用cocoon等等),就我所知,这些都是规范的问题。只要在新动作中构建子模型,它就会工作。

Argh!这真是一个美好的时刻。结果发现,ckeditor description字段上的
标记引起了所有这些麻烦。移除
标签可以让cocoon正常工作


因此,为了回答最初的问题:不,cocoon gem不要求在新的控制器操作中构建一个子模型来添加它们。

Ya您必须在新操作中“构建”至少一个子模型才能显示表单字段。你需要这样做,不管茧宝石是什么。cocoon gem所做的是动态添加更多字段。然后还需要嵌套的参数。你的强参数是什么样子的?这些参数进来时是什么样子的?谢谢你的反馈,迈克。强参数包括子字段并匹配提交的内容,但仅当在新操作中构建子模型时。如果我不创建子项,那么即使我在表单中添加字段,也不会返回任何内容。有点奇怪的是,gem中没有提到这个要求,演示应用程序中也没有。构建子项的要求是的
字段的要求。同样,您需要在新操作中使用
@model=model.new
,然后您需要初始化至少一个子项。是的,我也不知道,直到我的主管告诉我,当我不得不开始处理茧和巢田的时候。嗨,这不是我所期望的。您甚至不必在控制器中显式构建新的子项。呈现链接到添加新关联时,将创建一个新的子项(服务器端)。你可以给我们看一些代码或者看看演示项目。如果你怀疑这是一个bug,请在cocoon github上打开一个问题。嗨,Nathan:非常感谢你的反馈。如果这被证明是一个bug,那么很高兴打开一个问题。我想不是。目前,我倾向于认为我的jQuery设置不正确。我将用代码更新问题。
class Competition < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :event
  has_many :participants, dependent: :destroy
  belongs_to :type
  accepts_nested_attributes_for :participants, allow_destroy: true  

  <div class="text-left ">
    <%= simple_form_for @event do |f| %>
        <%= f.input :event_name, input_html: {maxlength: 60} %>
        <%= f.input :logo, label: "Event Logo:", hint: 'jpg or png files allowed, max size: 1MB' %>
        <a <%= f.input :description, label_html: {class: "glyphicon glyphicon-question-sign event-new", href: "#", 'data-content': "You can format your description using the editor buttons. Cutting and pasting from other text editors will not work unless they are first exported into html format. For security reasons, some html tags are not allowed and will be removed.", rel: "popover", "data-placement": 'top', 'data-original-title': 'WYSIWYG editor help', 'data-trigger': 'hover' }, as: :ckeditor, input_html: { ckeditor: { toolbar: 'mini' } } %></a>
        <%= f.input :event_address, placeholder: "Enter Street Address, City, State, Postal Code" %>
        <%= f.input :registration_fee, :input_html => { :value => '0.00'}, label: "Team registration fee" %>


        <%= f.input :event_start %>
        <%= f.input :event_end %>
        <br />
        <h3>Competitions</h3>
        <div id="competitions">
          <%= f.simple_fields_for :competitions do |competition| %>
            <%= render 'competition_fields', f: competition %>
          <% end %>
          <div class="links">
            <%= link_to_add_association 'add competition', f, :competitions %>
          </div>
        </div>

        <%= f.submit 'Create', :class => 'pull-right btn btn-primary' %>
    <% end %>
  </div

<div class="nested-fields">
  <%= f.input :competition_name %>
  <%= f.collection_select(:type_id, @types, :id, :name, prompt: "Select a Type") %>
  <%= f.input :fee, :input_html => { :value => '0.00'} %>
  <%= f.input :maximum_participants %>
  <%= link_to_remove_association "Delete Competition", f %>
</div>
//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require jquery-ui/widgets/autocomplete
//= require autocomplete-rails
//= require moment
//= require bootstrap-datetimepicker
//= require ckeditor/init
//= require google_analytics
//= require cocoon
//= require_tree .
"competitions_attributes"=>{"0"=>{"competition_name"=>"test1", "type_id"=>"2", "fee"=>"0.00", "maximum_participants"=>"8", "_destroy"=>"false"}}}, "commit"=>"Create"}

  SQL (17.4ms)  INSERT INTO "competitions" ("competition_name", "maximum_participants", "type_id", "fee", "event_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["competition_name", "test1"], ["maximum_participants", 8], ["type_id", 2], ["fee", "0.0"], ["event_id", 305], ["created_at", "2019-08-01 11:13:44.582299"], ["updated_at", "2019-08-01 11:13:44.582299"]]