我对 Ruby/Rails 还很陌生,我遇到了一个奇怪的问题,我似乎无法理解我到底做错了什么。
我认为我有以下代码
<% gameNum = 0 %>
gameNum: <%= gameNum %>
homeTeamIndex: <%= @games[gameNum].homeTeamIndex %>
awayTeamIndex: <%= @games[gameNum].awayTeamIndex %><br />
<%= @NflTeams[@games[gameNum].homeTeamIndex].name %>
<%= @NflTeams[@games[gameNum].awayTeamIndex].name %><br />
<% gameNum = 1 %>
gameNum: <%= gameNum %>
homeTeamIndex: <%= @games[gameNum].homeTeamIndex %>
awayTeamIndex: <%= @games[gameNum].awayTeamIndex %><br />
<%= @NflTeams[@games[gameNum].homeTeamIndex].name %>
<%= @NflTeams[@games[gameNum].awayTeamIndex].name %><br />
<% (0..@games.count).each do |gameNum| %>
gameNum: <%= gameNum %>
homeTeamIndex: <%#= @games[gameNum].homeTeamIndex %>
awayTeamIndex: <%#= @games[gameNum].awayTeamIndex %> <br />
<%#= @NflTeams[@games[gameNum].homeTeamIndex].name %>
<%#= @NflTeams[@games[gameNum].awayTeamIndex].name %>
<% end %>
当我查看我的视图时,我得到以下结果:
gameNum: 0 homeTeamIndex: 10 awayTeamIndex: 3
Detroit Lions Buffalo Bills
gameNum: 1 homeTeamIndex: awayTeamIndex:
Cinncinatti Bengals Cleveland Browns
gameNum: 0 homeTeamIndex: awayTeamIndex:
homeTeamIndex: awayTeamIndex:
gameNum: 1 homeTeamIndex: awayTeamIndex:
homeTeamIndex: awayTeamIndex:
gameNum: 2 homeTeamIndex: awayTeamIndex:
homeTeamIndex: awayTeamIndex:
gameNum: 3 homeTeamIndex: awayTeamIndex:
homeTeamIndex: awayTeamIndex:
gameNum: 4 homeTeamIndex: awayTeamIndex:
但是,如果我取消注释任何一行:
homeTeamIndex: <%#= @games[gameNum].homeTeamIndex %>
awayTeamIndex: <%#= @games[gameNum].awayTeamIndex %> <br />
<%#= @NflTeams[@games[gameNum].homeTeamIndex].name %>
<%#= @NflTeams[@games[gameNum].awayTeamIndex].name %>
我收到以下错误(方法名称根据我取消注释的行更改):
undefined method `homeTeamIndex' for nil:NilClass
我真的不明白循环中发生了什么导致实例变量不可用。
我希望有人能告诉我我到底做错了什么,因为这看起来确实是一件简单的事情,但我无法让它发挥作用。
更新
根据 Dmitry 的建议,我将观点改变如下:
<% gameNum = 0 %>
<% (@games).each do |game| %>
<strong>Game <%= gameNum+1 %> </strong> <br />
<%= image_tag(@NflTeams[game.homeTeamIndex].imagePath,
size: "40") %>
<%= @NflTeams[game.homeTeamIndex].name %>
VS
<%= image_tag(@NflTeams[game.awayTeamIndex].imagePath,
size: "40") %>
<%= @NflTeams[game.awayTeamIndex].name %> <br />
<% gameNum += 1 %>
<% end %>
型号如下:
橄榄球队
class CreateNflTeams < ActiveRecord::Migration
def change
create_table :nfl_teams do |t|
t.string :name
t.string :imagePath
t.timestamps
end
end
end
游戏
class CreateGames < ActiveRecord::Migration
def change
create_table :games do |t|
t.integer :homeTeamIndex
t.integer :awayTeamIndex
t.integer :spread
t.integer :week_id
t.timestamps
end
end
end
awayTeamIndex 和 homTeamIndex 是 NflTeams 模型的索引,因此我可以轻松提取 Name 和 ImagePath。
我仍然在线获得未定义的方法:
<%= image_tag(@NflTeams[game.homeTeamIndex].imagePath,
size: "40") %>
还有其他建议吗?
更新 2
我只包括迁移,因为这两个模型的模型并不多。但在这里。
class NflTeam < ActiveRecord::Base
end
class Game < ActiveRecord::Base
belongs_to :week
validates :homeTeamIndex, :inclusion => { :in => 0..100 }
validates :awayTeamIndex, :inclusion => { :in => 0..100 }
end
这是控制器代码:
class WeeksController < ApplicationController
before_action :signed_in_user
before_action :confirmed_user
...
def show
@week = Week.find(params[:id])
@games = @week.games
@NflTeams = NflTeam.all
end
...
end