【发布时间】: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