gpt4 book ai didi

javascript - 茧 gem 需要在 Controller 新 Action 中构建n个模型吗?

转载 作者:行者123 更新时间:2023-12-02 23:13:00 24 4
gpt4 key购买 nike

我已经安装了 cocoon gem在我的 Rails 4 应用程序中,完全按照描述进行。这在父模型表单中效果非常好,允许用户添加/删除子模型的字段。我遇到麻烦的地方是子对象的提交。如果子模型是在父模型的新操作中构建的,那么我可以准确提交创建的多个模型,仅此而已。从提交的参数中可以明显看出这一点,因为它们包含 child_attributes(如果 Controller 中没有构建子模型,则不包含 child_attributes)。

当前正在运行

rails 4.2.10

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

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 .

Image of the partial component in the form, note 2 competitions submitted

从 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 被调用等)。可以告诉。只要子模型是在新操作中构建的,它就可以工作。

最佳答案

啊啊!真是一个捂脸的时刻。结果是<a> ckeditor 描述字段上的标签引起了所有这些麻烦。删除<a>标签允许cocoon正常运行。

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

关于javascript - 茧 gem 需要在 Controller 新 Action 中构建n个模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57281291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com