首页 > 解决方案 > Accepts_nested_attributes_fields 没有为连接表显示

问题描述

我有以下表格

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 中显示详细信息,但不幸的是它没有。

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

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

任何帮助都深表感谢

标签: ruby-on-railsruby-on-rails-4simple-formcocoon-gem

解决方案


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

使用 SimpleForm,您真正需要的是:

<%= f.association :regions %>

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

<%= 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 来处理这些情况。


推荐阅读