Требует ли самоцвет кокона n моделей, чтобы встроить в контроллер новое действие?

Я установил камень кокона в свое приложение rails 4 точно так, как описано. Это прекрасно работает в форме родительской модели, позволяя пользователю добавлять / удалять поля для дочерних моделей. Где у меня проблемы, это представление дочернего объекта. Если дочерние модели встроены в новое действие родительской модели, то я могу представить точно, сколько моделей было создано, не более. Это очевидно из передаваемых параметров, так как они содержат child_attributes (или нет, если в контроллере не было построено ни одной дочерней модели).

В настоящее время работает

рельсы 4.2.10

рубин 2.5.1

кокон 1.2.14

рельсы jquery 4.3.3

jquery-ui-rails 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

Родительская модель

  acts_as_paranoid
  has_and_belongs_to_many :users
  has_many :competitions, dependent: :destroy, inverse_of: :event
  belongs_to :address
  accepts_nested_attributes_for :address
  accepts_nested_attributes_for :competitions, allow_destroy: true

Детская модель

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  

Форма (new.html.erb)

  <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

Частичное (с именем _competition_fields.html.erb

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

application.js

//= 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 .

Из консоли Rails (после вставки родительской модели)

"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"]]

Я прошел через все обычные проблемы с настройкой для гема (accepts_nested_attributes_for, inverse_of, именование и отступ частичного child_fields, jQuery установлен и вызван кокон и т. Д.) Это все, что я могу сказать, насколько я могу судить. И это работает, пока в новом действии встроены дочерние модели.

1 ответ

Решение

Argh! Какой момент лицевой стороны лица. Оказалось, <a> тег в поле описания ckeditor вызвал всю эту суету. Удаление <a> тег позволял кокону нормально работать.

Итак, в ответ на первоначальный вопрос: "Нет, жемчужина кокона не требует, чтобы в новом действии контроллера была встроена даже одна дочерняя модель, чтобы добавить их.

Другие вопросы по тегам