私は熱狂的な Rails 初心者で、has-many-through 結合テーブルのフィールドを編集する方法を理解しようとしています。
私は3つのモデルを持っています。
プレーヤー モデル:
# == Schema Information
#
# Table name: players
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class Player < ActiveRecord::Base
attr_accessible :name
has_many :appearances #, :dependent => true
has_many :games, :through => :appearances
accepts_nested_attributes_for :games
accepts_nested_attributes_for :appearances
end
ゲームモデル:
# == Schema Information
#
# Table name: games
#
# id :integer not null, primary key
# team :string(255)
# date :date
# created_at :datetime not null
# updated_at :datetime not null
# game_innings :integer
#
class Game < ActiveRecord::Base
attr_accessible :date, :team, :game_innings
has_many :appearances #, :dependent => true
has_many :players, :through => :appearances
accepts_nested_attributes_for :players
accepts_nested_attributes_for :appearances
end
そして外観モデル:
# == Schema Information
#
# Table name: appearances
#
# id :integer not null, primary key
# player_id :integer not null
# game_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# innings_played :integer
#
class Appearance < ActiveRecord::Base
attr_accessible :player_id, :game_id
belongs_to :game
belongs_to :player
end
プレーヤーを編集するとき、各ゲームでプレイしたイニングを一覧表示して編集できるようにしたいと考えています。具体的には、各外観の innings_played を一覧表示して編集したいと考えています。
私はそれが不完全であることを理解していますが、現時点で私の edit.html.erb は次のようになっています:
<h1>Editing player</h1>
<%= form_for(@player) do |f| %>
<% if @player.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@player.errors.count, "error") %> prohibited this player from being saved:</h2>
<ul>
<% @player.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<table>
<tr>
<th>Opponent</th>
<th>Innings Played</th>
</tr>
<% f.fields_for :games do |games_form| %>
<tr>
<td><%= games_form.label :team %></td>
</tr>
<% end %>
</table>
<br>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Show', @player %> |
<%= link_to 'Back', players_path %>
共有できる知識に感謝します。