我是 ruby 和 rails 的新手,几天来一直在努力解决这个问题。我已经搜索了答案,但找不到解决方案。
我通过 has_many :through 关联在游戏、代码和玩家之间建立了多对多的联系。相关的关联声明可能如下所示:
class Game < ActiveRecord::Base
has_many :tickers, :dependent => :destroy
has_many :players, :through => :tickers
end
class Ticker < ActiveRecord::Base
belongs_to :games
belongs_to :players
end
class Player < ActiveRecord::Base
has_many :tickers, :dependent => :destroy
has_many :games, :through => :tickers
end
这意味着每场比赛都有很多代码。每个 Ticker 都有一个玩家。最后,带有代码条目的游戏可能如下所示:
团队 1 与团队 2
00:30 | 目标 | 玩家 1
01:30 | 目标 | 玩家 2……</p>
我想在游戏视图中显示玩家姓名。这是我的应用程序/视图/游戏/show.html.erb:
<p id="notice"><%= notice %></p>
<p>
<b>Home:</b>
<%= @game.home_team.team_name %>
</p>
<p>
<b>Away:</b>
<%= @game.away_team.team_name %>
</p>
<table>
<tr>
<th>Activity</th>
<th>Player</th>
<th>Time</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @tickers.each do |ticker| %>
<tr>
<td><%= ticker.activity_id %></td>
<td><%= ticker.players.player_name %></td>
<td><%= ticker.time %></td>
<td><%= link_to 'Show', ticker %></td>
<td><%= link_to 'Edit', edit_ticker_path(ticker) %></td>
<td><%= link_to 'Destroy', ticker, confirm: 'Are you sure?', method: :delete %> </td>
</tr>
<% end %>
</table>
这是我的问题:
<%= ticker.players.player_name %>
给我错误
nil:NilClass 的未定义方法“player_name”
但
<%= ticker.player_id %>
给我正确的玩家ID。具有此 id 的玩家存在并设置了 player_name。所以,这不可能是原因。但是我打电话时得到零:
<%= ticker.players %>
什么地方出了错?谢谢你的帮助!
拉斯
PS:有关我的代码的更多信息:
app/controllers/games_controller.rb:
def show
@game = Game.find(params[:id])
@tickers = @game.tickers
respond_to do |format|
format.html # show.html.erb
format.json { render json: @game }
end
end
我的数据库架构:
create_table "games", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "players", :force => true do |t|
t.string "player_name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "tickers", :force => true do |t|
t.integer "activity_id"
t.integer "player_id"
t.integer "time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "game_id"
end
这就是我创建一个新的ticker实例的方式(现在):
应用程序/视图/游戏/show.html.erb:
<%= link_to 'New Ticker', new_ticker_path(:game_id=>@game.id) %>
app/views/tickers/_form.html.erb
<%= form_for(@ticker) do |f| %>
<% if @ticker.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ticker.errors.count, "error") %> prohibited this ticker from being saved:</h2>
<ul>
<% @ticker.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.hidden_field :game_id, :value => params[:game_id] %>
</div>
<div class="field">
<%= f.label :activity_id %><br />
<%= f.number_field :activity_id %>
</div>
<div class="field">
<%= f.label :player_id %><br />
<%= f.number_field :player_id %>
</div>
<div class="field">
<%= f.label :time %><br />
<%= f.number_field :time %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
应用程序/控制器/tickers_controller.rb
def new
@ticker = Ticker.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @ticker }
end
end
def create
@ticker = Ticker.new(params[:ticker])
respond_to do |format|
if @ticker.save
format.html { redirect_to @ticker, notice: 'Ticker was successfully created.' }
format.json { render json: @ticker, status: :created, location: @ticker }
else
format.html { render action: "new" }
format.json { render json: @ticker.errors, status: :unprocessable_entity }
end
end
end