Rails 5 inverse_of联接表不希望与深度嵌套的Cocoon字段一起使用

问题描述 投票:0回答:1

我正在使用Ruby 2.6.5Rails 5.2.3。我有一个带有Cocoon的双嵌套对象的表单。该表格适用于Preorders。一个Preorder可以有多个DancersDancers可以有很多并且属于许多Groups,因此我建立了一个名为DancerGroupjoin table

我使用Cocoondancers_attributes添加到Preorder表单,并再次在dancer_groups_attributes字段内添加dancers_attributes

我的问题是,当我尝试用新的Preorder和新的Dancer关联保存DancerGroup时,出现以下错误:

  • 舞者舞者团体舞者不能空白

这显然意味着它正在尝试在dancer_group记录具有dancer之前保存id对象。显而易见的答案是,由于我使用的是Rails 5,因此默认情况下需要belongs_to关联,并且解决方法是正确使用inverse_ofaccepts_nested_attributes_for方法让Rails一次保存所有内容。关于[但是不起作用。在optional: true模型中的belongs_to :dancer上设置DancerGroup时,甚至会遇到相同的错误。

我的[[强参数看起来像这样:

private def preorder_params params.require(:preorder).permit( :event_id, :dancers_attributes => [ :_destroy, :id, :name, :dancer_groups_attributes => [ :_destroy, :group_id, :id ] ] ) end 我的[[模型看起来像这样:

app / models / preorder.rb

class Preorder < ApplicationRecord belongs_to :event has_many :dancers, dependent: :destroy, inverse_of: :preorder has_many :dancer_groups, through: :dancers, dependent: :destroy with_options reject_if: :all_blank, allow_destroy: true do accepts_nested_attributes_for :dancers accepts_nested_attributes_for :dancer_groups end validates :dancers, :length => { minimum: 1 } ... end

app / models / dancer.rb

class Dancer < ApplicationRecord
  belongs_to :preorder, counter_cache: true, inverse_of: :dancers
  has_many :dancer_groups, inverse_of: :dancer, dependent: :destroy
  has_many :groups, through: dancer_groups

  with_options reject_if: :all_blank, allow_destroy: true do
    accepts_nested_attributes_for :dancer_groups
  end
  ...
end

app / models / dancer_group.rb

class DancerGroup < ApplicationRecord
  belongs_to :dancer, inverse_of :dancer_groups
  belongs_to :group, inverse_of :dancer_groups
  ...
end

这是我的表单文件的样子

views / admin / preorders / _form.html.haml

... %fieldset %legend Dancers #dancers - @preorder.dancers.each do |dancer| = f.fields_for :dancers, dancer do |ff| = render "dancer_fields", f: ff, preorder: @preorder = link_to_add_association f, :dancers, { "data-association-insertion-node": "#dancers", "data-association-insertion-method": "append", render_options: { locals: { preorder: @preorder } }, class: "btn btn-info btn-sm btn-simple float-right" } do = icon "fas", "plus" Add Dancer ...

views / admin / preorders / _dancer_fields.html.haml

...
.card.preorder_dancer
  .card-header.row
    .col
      %h4.card-title Dancer Info
    .col-auto
      = link_to_remove_association f, { wrapper_class: "preorder_dancer",
          class: "btn btn-sm btn-icon btn-simple btn-danger" } do 
        = icon "fas", "times"
  .card-body
    .row.pb-3
      .col 
        = f.label :name do 
          Full Name
          %span.text-danger *
        = f.text-field :name, class: "form-control"
    %h4.card-title Dance Groups
    .row{ :id => "dancer_groups_#{f.object.id}" }
      - f.object.dancer_groups.each do |dancer_group|
        = f.fields_for :dancer_groups, dancer_group do |ff|
          = render "dancer_group_fields, f: ff, 
                                  preorder: preorder
    =link_to_add_association f, :dancer_groups,
      { "data-association-insertion-node": "#dancer_groups_#{f.object.id}",
        "data-association-insertion-method": "append",
        render_options:  {locals: { preorder: preorder } },
        class: "btn btn-info btn-sm btn-simple float-right" } do
      =icon "fas", "plus"
      Add Dance Group

views / admin / preorders / _dancer_group_fields.html.haml

.col-lg-6.preorder_dancer_group
  .card.bg-dark
    .card-header.row
      .col
        %h4.card-title Dance Group
      .col.text-right
        =link_to_remove_association f, { wrapper_class: "preorder_dancer_group",
                                    class: "btn btn-sm btn-icon btn-simple btn-danger"} do
          =icon "fas", "times"
    .card-body
      .row
        .col
          =f.label :group_id do
            Group
            %span.text-danger *
          =f.collection_select :group_id,
                               preorder.event.groups,
                               :id,
                               :name,
                               {include_blank: "Select Group"},
                               {class: "form-control"}
ruby-on-rails ruby join cocoon-gem
1个回答
0
投票
并且应该是

%h4.card-title Dance Groups .row{ :id => "dancer_groups_#{f.object.id}" } = f.fields_for :dancer_groups do |ff| = render "dancer_group_fields, f: ff, preorder: preorder

因为否则您将不会将舞者组“连接”到舞者。    
© www.soinside.com 2019 - 2024. All rights reserved.