我已经完全按照描述在我的 rails 4 应用程序中安装了cocoon gem 。这在父模型表单中非常有效,允许用户为子模型添加/删除字段。我遇到麻烦的地方是子对象的提交。如果子模型是在父模型的新操作中构建的,那么无论创建了多少模型,我都可以准确地提交,仅此而已。从提交的参数中可以明显看出这一点,因为它们包含 child_attributes(或者不包含,如果控制器中没有构建子模型)。
目前正在运行
导轨 4.2.10
红宝石 2.5.1
茧 1.2.14
jquery-rails 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>
应用程序.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"]]
我已经解决了 gem 设置的所有常见问题(accepts_nested_attributes_for、inverse_of、child_fields 部分的命名和缩进、安装了 jQuery 并调用了 cocoon 等)据我所知,这一切都符合规范。只要在新动作中构建子模型,它就可以工作。