我创建了2个模型:
class Fixture < ActiveRecord::Base
attr_accessible :away_score, :away_team_id, :home_score, :home_team_id, :result, :week
belongs_to :home_team, :class_name => Team
belongs_to :away_team, :class_name => Team
end
class Team < ActiveRecord::Base
attr_accessible :form, :name
has_many :fixtures, :class_name => Fixture, :foreign_key => :home_team_id
has_many :fixtures, :class_name => Fixture, :foreign_key => :away_team_id
end
然后我在我的灯具表单中完成了一个collection_select,它允许我获取团队列表,但是当我尝试创建一个灯具时,它不会更新我的灯具表中的away_team_id和home_team_id,我相信我需要在我的fixtures控制器创建方法中添加一些代码,但不确定我需要添加什么?
<%= form_for(@fixture) do |f| %>
<% if @fixture.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@fixture.errors.count, "error") %> prohibited this fixture from being saved:</h2>
<ul>
<% @fixture.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :week %><br />
<%= f.text_field :week %>
</div>
<div class="field">
<%= f.label :home_team_id %><br />
<%= collection_select(:team, :home_team_id, Team.all,:id,:name,:prompt => 'Please select a team') %>
</div>
<div class="field">
<%= f.label :away_team_id %><br />
<%= collection_select(:team, :away_team_id, Team.all,:id,:name,:prompt => 'Please select a team') %>
</div>
<div class="field">
<%= f.label :home_score %><br />
<%= f.number_field :home_score %>
</div>
<div class="field">
<%= f.label :away_score %><br />
<%= f.number_field :away_score %>
</div>
<div class="field">
<%= f.label :result %><br />
<%= f.text_field :result %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>