Ruby on rails 3.2 如何通过联接表编辑多个字段

Ruby on rails 3.2 如何通过联接表编辑多个字段,ruby-on-rails-3.2,inner-join,has-many-through,Ruby On Rails 3.2,Inner Join,Has Many Through,我是一个热衷于Rails的新手,试图弄清楚如何编辑has-many-through-join表中的字段 我有三种型号 玩家模型: # == Schema Information # # Table name: players # # id :integer not null, primary key # name :string(255) # created_at :datetime not null # updated_at

我是一个热衷于Rails的新手,试图弄清楚如何编辑has-many-through-join表中的字段

我有三种型号

玩家模型:

# == 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
#==架构信息
#
#表名:玩家
#
#id:整数不为空,主键
#名称:字符串(255)
#创建时间:datetime非空
#更新时间:datetime非空
#
类播放器真
有很多:游戏,:通过=>:出场
接受游戏的\u嵌套\u属性\u
接受:外观的\u嵌套\u属性\u
结束
游戏模型:

# == 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
#==架构信息
#
#表名:游戏
#
#id:整数不为空,主键
#团队:字符串(255)
#日期:日期
#创建时间:datetime非空
#更新时间:datetime非空
#局数:整数
#
类游戏真
有很多:球员,:通过=>:出场
为:玩家接受\u嵌套的\u属性\u
接受:外观的\u嵌套\u属性\u
结束
和外观模型:

# == 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
#==架构信息
#
#表名称:外观
#
#id:整数不为空,主键
#玩家id:整数不为空
#游戏id:整数不为空
#创建时间:datetime非空
#更新时间:datetime非空
#你打的局数:整数
#
类外观
当我编辑一名球员时,我希望能够列出和编辑每场比赛的局数,或者更具体地说,我希望列出和编辑每场比赛的局数

我意识到它是不完整的,但我的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 %>
编辑播放器
禁止保存此玩家:

对手 局数

|

感谢您能分享的所有知识。

更改玩家和外观如下,请注意每个类顶部的
:appearancess\u attributes
:game\u 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
classplayer真
有很多:游戏,:通过=>:出场
接受:外观的\u嵌套\u属性\u
结束
类外观
按以下方式更改您的视图,因为我不知道您希望在视图上显示什么,所以我只举一个例子,您可能需要更改它以满足您的情况

<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>

局数
团队

做得很好!非常感谢你!如果我只想列出团队名称而不使其可编辑,那么如何从块的字段_中进行编辑?换言之,与其创建文本字段,不如直接显示名称?只需删除游戏
appearancess\u form.fields\u,并用
appearancess\u form.object.game.team
Ah替换游戏{u form.text\u字段即可。我离得如此近,却又如此遥远。要学的东西太多了。非常感谢。我原以为。object会解决这个难题,但当我回家尝试时,我收到了nil:NilClass的未定义方法“team”。我可以用form.object.player.name做外观。我的模型中有什么遗漏吗?