0

私は熱狂的な 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 %>

共有できる知識に感謝します。

4

1 に答える 1

0

プレーヤーと外観を次のように変更します。各クラスの上部に注意:appearances_attributesしてください。:game_attributes

class Player < ActiveRecord::Base
  attr_accessible :name, :appearances_attributes

  has_many :appearances #, :dependent => true
  has_many :games, :through => :appearances

  accepts_nested_attributes_for :appearances
end

class Appearance < ActiveRecord::Base
  attr_accessible :player_id, :game_id, :innings_played, :game_attributes

  belongs_to :game
  belongs_to :player

  accepts_nested_attributes_for :game
end

ビューに何を表示したいのか正確にはわからないため、ビューを次のように変更します。例を示します。状況に合わせて変更する必要がある場合があります。

<table>
   <tr>            
   <th>Innings Played</th>
   <th>Team</th>
   </tr>
     <%= f.fields_for :appearances do |appearances_form|%>                              

       <%= appearances_form.fields_for :game do |game_form| %>
         <tr>                               
            <td><%= appearances_form.text_field :innings_played %></td>
            <td><%= game_form.text_field :team %></td>
         </tr>
       <% end %>

    <% end %>

</table>
于 2013-06-30T10:54:12.407 に答える