【问题标题】:accepts_nested_attributes_fields not showing for join tableAccepts_nested_attributes_fields 没有为连接表显示
【发布时间】:2018-05-05 00:38:54
【问题描述】:

我有以下表格

class Region < ActiveRecord::Base
  has_many :companies, through: :companies_regions
  has_many :companies_regions, :dependent => :destroy
end

class Company < ActiveRecord::Base
  has_many :regions, through: :companies_regions
  has_many :product_type, dependent: :destroy
  has_many :companies_regions, :dependent => :destroy

  accepts_nested_attributes_for :companies_regions, :allow_destroy => true
end

class CompaniesRegion < ActiveRecord::Base
  belongs_to :company
  belongs_to :region
end

我想创建一家新公司,并且我希望能够将新区域相应地添加到 CompaniesRegion 表中。

form.html.erb

<%= simple_form_for(['admin', @company]) do |f| %>
  <%= f.error_notification %>
  <div class="form-group">
    <%= f.input :name %>
  </div>
  <div>
  <div class="row">
    <div class="col-md-12">
      <h4>Basic Coverages</h4>
      <div class="row form-group">
        <label class="col-md-1">#</label>
        <label class="col-md-3">Coverage</label>
        <label class="col-md-1">Description</label>
      </div>
      <div>
        <%= f.simple_fields_for :companies_regions do |company_region| %>
          <%= render 'company_region', f: company_region  %>
        <% end %>
        <%= link_to_add_association 'New Region', f, :companies_regions, partial: 'company_region' %>
      </div>
    </div>
  </div>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

_company_region.html.erb

<div class="nested-fields form-group row">
  <div class="col-md-1"></div>
  <div class="col-md-3">
    <%#= f.select :region_id, class: 'form-control', placeholder: 'Region' %>
    <%= f.input_field :region_id, collection: ['Asia', 'America'], class: 'form-control', prompt: 'Please Select' %>
  </div>
  <div class="col-md-1">
    <%= link_to_remove_association(f, title: 'Remove') do  %>
      <span class="glyphicon glyphicon-remove"></span>
    <% end %>
  </div>
</div>

这里的问题是,当我单击“新区域”链接时,我希望它会在 _company_region.html.erb 中显示详细信息,但不幸的是它没有。

没有显示。它不显示任何数据。然而,它刷新了荒谬的页面。

不知道是不是因为我的表是一个连接表,因此出现了问题,或者是否还有其他我遗漏的东西,但根据文档,这应该没问题并且应该可以工作。

非常感谢任何帮助

【问题讨论】:

  • 请在问题中包含 _company_region 部分。 partial: 是必需的,因为部分不遵循命名约定(company_region_fields)

标签: ruby-on-rails ruby-on-rails-4 simple-form cocoon-gem


【解决方案1】:

您需要使用嵌套属性来设置关联,这是一个非常常见的误解。你没有。 Rails 为关联生成一个regions_ids= 设置器,可以与复选框或选择标签挂钩。

SimpleForm 真正需要的是:

<%= f.association :regions %>

有关其工作原理的更多详细信息,请参阅ActionView::Helpers::FormOptionsHelperSimpleForm 中的关联文档。

<%= simple_form_for(['admin', @company]) do |f| %>
  <%= f.error_notification %>
  <div class="form-group">
    <%= f.input :name %>
  </div>
  <div>
  <div class="row">
    <div class="col-md-12">
      <h4>Basic Coverages</h4>
      <div class="row form-group">
        <label class="col-md-1">#</label>
        <label class="col-md-3">Coverage</label>
        <label class="col-md-1">Description</label>
      </div>
      <div>
        <%= f.association :regions %>
      </div>
    </div>
  </div>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

您需要使用嵌套属性的唯一原因是用户必须能够在同一请求中创建区域。但通常最好使用 ajax 来处理这些情况。

【讨论】:

    【解决方案2】:

    您的代码看起来不错。您描述的行为似乎表明 cocoon.js 代码未正确包含/加载?你有没有

    • application.js 中添加require 'cocoon'
    • 您是否在 html 中包含 application.js

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2013-08-01
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多