Ruby on rails Rails 4在显示页面上有许多通过编辑的关联
我正在构建Rails 4应用程序,其中我有2个模型和1个用于关联: game.rbRuby on rails Rails 4在显示页面上有许多通过编辑的关联,ruby-on-rails,model-associations,Ruby On Rails,Model Associations,我正在构建Rails 4应用程序,其中我有2个模型和1个用于关联: game.rb class Game < ActiveRecord::Base has_many :participations has_many :players, :through => :participations accepts_nested_attributes_for :participations, :allow_destroy => true acce
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
类游戏:参与
接受\u嵌套的\u属性\u for:participations,:allow\u destroy=>true
为:玩家接受\u嵌套的\u属性\u
结束
player.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
classplayer:参与
接受\u嵌套的\u属性\u for:participations,:allow\u destroy=>true
接受游戏的\u嵌套\u属性\u
结束
参与.rb
class Participation < ActiveRecord::Base
belongs_to :player
belongs_to :game
accepts_nested_attributes_for :game
end
课堂参与
其理念是,几乎每天都有游戏进行,玩家在每场游戏中都会获得分数(这一点保存在参与模型中)
如果我进入“游戏”页面,选择一个游戏a查看所有参与该游戏的玩家及其分数,一切正常
我似乎无法在游戏展示页面上添加玩家及其分数。
在玩家列表之后,需要一个带有选择器(玩家名称可供选择)和文本框的表单来输入分数
如果这可以用Ajax实现,那就更好了
任何帮助都将不胜感激
更新:
在我的show视图中,可以显示球员及其得分:
<% @game.participations.each do |participation| %>
<tr>
<td><%= participation.player.name %></td>
<td><%= participation.score %></td>
<td><%= participation.time %></td>
</tr>
<% end %>
更新2
所以我在游戏中得到了一些效果#显示页面如下所示:
<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 %>
日期:
时间:
|
球员
玩家
分数
时间
参与度路径:html=>{:method=>:post}do | f |%>
现在一切都正常了,但我如何将游戏id传递给表单,因为我在游戏中#show view I have@game.id我尝试了类似的方法,但没有成功:
<%= form_for :participation, :url => participations_path, :html => {:method => :post}, :game_id => @game.id do |f| %>
participations_path,:html=>{:method=>:post},:game_id=>@game.id do | f |%>
您的关联混淆了。当您设置has_many,:through=>关联时,需要关联的模型来链接模型-在您的例子中是“participations”
它只是一个链接,您不能通过@game.participations直接查看所有游戏。您需要参考其他模型,因此在您的案例中@game.players。例如:
<% @game.players.each do |player| %>
<tr>
<td><%= player.name %></td>
<td><%= player.score %></td>
<td><%= player.time %></td>
</tr>
<% end %>
更新:
如果您想知道如何为与AJAX有很多关联的应用程序添加字段,我建议您查看这两个RailsCast。我在最近的一个项目中使用了这种方法,效果非常好:
查看宝石。@nathanvda我会看一看,然后告诉你。我尝试了cocoon,但我得到了一个错误,未定义NilClass:classe的方法“reflect\u on\u association”(反映在\u关联上)。我所有的协会都运作良好。因为我现在正在测试它们,一切正常。另外,我的玩家模型没有分数或时间字段,这些字段在参与模型中(因为对于每一个游戏,它都是唯一的分数和玩的时间),我希望能够从show game viewAha中将玩家添加到特定的游戏中。你在原始帖子中的参与模式并没有表明这一点,因此我产生了误解。请看我原来帖子的更新。
accepts_nested_attributes_for :participations, :allow_destroy => true