我想显示一个表单来添加或编辑结果(如果存在)。我有一个 n * n 的表格,其中单元格中的 n 个团队由结果给出。这有多好?
view/championship/show.erb
<table class="table table-bordered">
<tr><th> </th>
<% @teams.each do |teamhome| %>
<th class="vertical"><%= teamhome.name %></th>
<% end %>
</tr>
<% @teams.each do |teamguest| %>
<tr>
<th><%= teamguest.name %></th>
<% @teams.each do |teamhome| %>
<%if teamhome == teamguest %>
<td bgcolor = '#F9F9F9'>
<% else %>
<td>
#render form
</td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
楷模
class Score < ActiveRecord::Base
attr_accessible :team1_score, :team2_score, :team1_id, :team2_id, :team1, :team2
belongs_to :team1, class_name: 'Team', foreign_key: :team1_id
belongs_to :team2, class_name: 'Team', foreign_key: :team2_id
def self.to_arr
score_arr = self.find(:all, :select => 'team1_id, team2_id, team1_score, team2_score').to_a
end
end
class Team < ActiveRecord::Base
attr_accessible :name
belongs_to :championship
has_many :team1_scores, class_name: 'Score', foreign_key: :team1_id
has_many :team2_scores, class_name: 'Score', foreign_key: :team2_id
validates :name, presence: true, length: { maximum: 100 }
validates :championship_id, presence: true
end
图式
“team1_id”|“team2_id”|“team1_score”|“team2_score”