【问题标题】:Rails 4 has_many through edit association on the show pageRails 4 has_many 通过显示页面上的编辑关联
【发布时间】:2023-03-17 08:59:01
【问题描述】:

我正在构建一个 Rails 4 应用程序,其中有 2 个模型和 1 个用于关联:

游戏.rb

    class Game < ActiveRecord::Base
    has_many :participations
    has_many :players, :through => :participations

    accepts_nested_attributes_for :participations, :allow_destroy => true
    accepts_nested_attributes_for :players
end

播放器.rb

class Player < ActiveRecord::Base
    validates :name, presence: true, uniqueness: true

    has_many :participations
    has_many :games, :through => :participations

    accepts_nested_attributes_for :participations, :allow_destroy => true
    accepts_nested_attributes_for :games
end

参与.rb

class Participation < ActiveRecord::Base
    belongs_to :player
    belongs_to :game

    accepts_nested_attributes_for :game
end

这个想法是,几乎每天都在玩游戏,玩家每场比赛都会获得分数(这保留在参与模型中)。

如果我进入游戏页面选择一个游戏并查看所有参与该游戏的玩家及其得分,一切正常。

我似乎无法在游戏节目页面上添加玩家和他们的分数。 与球员列表后,想要一个带有选择器(选择球员姓名)和输入分数的文本框的表格。

如果这可以用 Ajax 来完成会更好。

任何帮助将不胜感激。

更新:

我在我的显示视图中有这个来显示玩家和他们的分数:

<% @game.participations.each do |participation| %>
      <tr>
        <td><%= participation.player.name %></td>
        <td><%= participation.score %></td>
        <td><%= participation.time %></td>
      </tr>
  <% end %>

更新 2

所以我的 Game#show 页面看起来像这样:

<p id="notice"><%= notice %></p>

<p>
  <strong>Date:</strong>
  <%= @game.date %>
</p>

<p>
  <strong>Time:</strong>
  <%= @game.time %>
</p>

<%= link_to 'Edit', edit_game_path(@game) %> |
<%= link_to 'Back', games_path %>

<hr>
<h4>Players</h4>
<hr>


<table class="table table-condensed">
  <thead>
    <tr>
      <th>Player</th>
      <th>Score</th>
      <th>Time</th>
    </tr>
  </thead>

  <tbody>
  <% @game.participations.each do |participation| %>
      <tr>
        <td><%= participation.player.name %></td>
        <td><%= participation.score %></td>
        <td><%= participation.time %></td>
      </tr>
  <% end %>
  </tbody>
</table>

<%= form_for :participation, :url => participations_path, :html => {:method => :post} do |f| %>
<table>
<tr>
  <td><%= f.collection_select :player_id, Player.all, :id, :name %></td>
  <td><%= f.text_field :score %></td>
  <td><%= f.text_field :time %></td>
</tr>
</table>
<%= f.submit  %>
<% end %>

现在一切正常,但我如何将 game_id 传递给表单,因为我在 Game#show 视图中我有 @game.id 我尝试过类似的操作但不起作用:

<%= form_for :participation, :url => participations_path, :html => {:method => :post}, :game_id => @game.id do |f| %>

【问题讨论】:

  • 查看cocoon gem。
  • @nathanvda 我会去看看,让你知道。
  • 我试过 cocoon 但我得到这个错误 undefined method `reflect_on_association' for NilClass:Class

标签: ruby-on-rails model-associations


【解决方案1】:

你把你的联想搞混了。当您设置 has_many, :through => 关联时,需要关联模型来链接模型 - 在您的情况下为“参与”。

它仅作为一个链接,您无法通过@game.participations 直接查看所有游戏。您需要引用其他模型,因此在您的情况下为 @game.players。例如:

<% @game.players.each do |player| %>
      <tr>
        <td><%= player.name %></td>
        <td><%= player.score %></td>
        <td><%= player.time %></td>
      </tr>
  <% end %>

此外,“参与”不需要嵌套属性方法,它只是一个链接模型:

accepts_nested_attributes_for :participations, :allow_destroy => true

更新:

如果您想知道如何为与 AJAX 的 has_many 关联添加字段,我建议您查看这两个 Railscast。我在最近的一个项目中使用了这种方法,效果很好:

Nested forms: Part 1

Nested forms: Part 2

【讨论】:

  • 也许你误会了我。我所有的协会工作正常。因为我现在正在测试它们,所以没关系。另外我的玩家模型没有分数或时间字段,这个字段在参与模型中(因为对于每场比赛,它都是唯一的分数和时间)我希望能够将玩家添加到节目中的特定游戏游戏视图
  • 啊哈,我明白了。您在原始帖子中的参与模型并未表明这一点,因此我产生了误解。请查看我原始帖子的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 2013-08-20
  • 1970-01-01
  • 2014-12-16
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多