【问题标题】:How to edit fields in has-many-through join table如何编辑有多个连接表中的字段
【发布时间】:2013-06-27 11:37:34
【问题描述】:

我是一个热心的 Rails 新手,我正在尝试弄清楚如何编辑 has-many-through 连接表中的字段。

我有三个模型。

玩家模型:

# == 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 %>

感谢您分享的任何知识。

【问题讨论】:

    标签: ruby-on-rails-3.2 inner-join has-many-through


    【解决方案1】:

    更改播放器和外观如下,请注意每个班级顶部的: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>
    

    【讨论】:

    • 做得很完美!非常感谢!
    • 如果我只想列出团队名称而不让它们可编辑,我该如何在 fields_for 块中做到这一点?换句话说,与其将 设为 text_field,不如只显示名称?
    • 只需删除appearances_form.fields_for :game,并将game_form.text_field 替换为appearances_form.object.game.team
    • 啊。我如此接近,却又如此遥远。这么多要学。非常感谢。
    • 我认为 .object 会解决这个难题,但是当我回家尝试时,我收到了 nil:NilClass 的未定义方法 `team'。我可以做appearances_form.object.player.name。我的模型中是否缺少某些内容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多