我在公司和行业之间建立了多对多的关系,因此当我创建公司时,我可以从公司的一个或多个行业的精选集合中进行选择。创建公司并重定向到公司页面后,我无法获得公司的行业信息。有人可以帮我理解为什么吗?
这是我公司的模特
class Company < ActiveRecord::Base
has_many :industres
has_many :industies, through: :industrializations
end
这是我的行业模式
class Industry < ActiveRecord::Base
has_many :industrializations
has_many :companies, through: :industrializations
end
这是我的工业化模式
class Industrialization < ActiveRecord::Base
belongs_to :company
belongs_to :industry
end
这是我的_form.html.erb
<%= f.label :industry %><br />
<%= f.collection_select :industry_ids, Industry.all, :id, :name, :prompt => "Choisir votre secteur d'activité" %>
这是我的公司show.html.erb
<p>
<strong>Name:</strong>
<%= @company.name %>
</p>
<p>
<strong>Description:</strong>
<%= @company.description %>
</p>
<p>
<strong>Country:</strong>
<%= @company.country %>
</p>
<p>
<strong>City:</strong>
<%= @company.city %>
</p>
<b>Sector:</b>
<ul>
<% @company.industries.each do |industry| %>
<li><%= industry.name %></li>
<% end %>
</ul>
这是我的架构
create_table "companies", force: true do |t|
t.string "name"
t.text "description"
t.string "country"
t.string "city"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "industrializations", force: true do |t|
t.integer "company_id"
t.integer "industry_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "industrie", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
行业名称在种子文件中
公司模型是通过脚手架创建的
答案 0 :(得分:1)
您已将此标记为Rails 3.2和Rails 4,但它们各自有不同的方式通过控制器接受表单信息到模型。
在Rails 3.2中,您必须确保将industry_ids属性标记为attr_accessible。但是,既然你没有抱怨“大规模分配”错误,我认为你可能在Rails 4上。
在Rails 4中,你现在需要使用'强参数'来允许params进入模型 - 你可能会发现脚手架控制器已经为公司的其他属性做了这个,你只需要添加industry_ids来允许它们。