Rails belongs_to关联在视图中返回nil

时间:2013-06-18 17:21:08

标签: ruby-on-rails-3.2

我是红宝石和铁轨的新手,几天来一直在努力解决这个问题。我已经找到了答案但找不到解决方案。

我通过has_many:通过关联在游戏,代码和玩家之间建立了多对多的联系。相关的协会声明可能如下所示:

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

我想在游戏视图中显示玩家姓名。这是我的app / views / games / 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 %>

给我错误

  

未定义的方法`player_name&#39;为零:NilClass

但是

<%= ticker.player_id %>

给了我正确的玩家ID。具有此ID的播放器存在且设置了player_name。所以,这不是原因。但是在打电话时我没有结果:

<%= ticker.players %>

出了什么问题?谢谢你的帮助!

拉​​斯

P.S。:关于我的代码的更多信息:

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

这就是我创建新的自动收报器实例的方法(暂时):

app / views / games / 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 %>

app / controllers / 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

1 个答案:

答案 0 :(得分:4)

belongs_to应该是单数。即

belongs_to :game #u have specified 'games'