1

私は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

インスタンス変数を使用不可にするループ内で何が起こっているのか、私は本当に理解していません。

これは確かにビューで行うのは簡単なことのように思え、私はそれを機能させることができないので、誰かが私が間違っていることを教えてくれることを願っています。

アップデート

ドミトリーのアドバイスに従って、私は次のように見解を変更しました。

<% gameNum = 0 %>
<% (@games).each do |game| %>
  <strong>Game <%= gameNum+1 %>  </strong> <br />
  <%= image_tag(@NflTeams[game.homeTeamIndex].imagePath,
                size: "40") %>
  <%= @NflTeams[game.homeTeamIndex].name %>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VS
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <%= image_tag(@NflTeams[game.awayTeamIndex].imagePath,
                size: "40") %>
  <%= @NflTeams[game.awayTeamIndex].name %> <br />
  <% gameNum += 1 %>
<% end %>

モデルは次のとおりです。

Nflチーム

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

これら 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
4

1 に答える 1

0

You are not accessing you @games in a loop correctly. It looks like you are thinking about gameNum in this line:

<% (0..@games.count).each do |gameNum| %>  

as if it is a numerical index and using it to access an object in a collection, in the following line:

homeTeamIndex: <%#= @games[gameNum].homeTeamIndex %>

This is wrong because "gameNum" is actually your object from a collection of @games fetched for you by magic ruby, so what you need is something like this:

<% (@games).each do |game| %>
  gameNum: <%= game.number %>
  homeTeamIndex: <%= game.homeTeamIndex %>
  awayTeamIndex: <%= game.awayTeamIndex %> <br />
  <%= game.homeTeam.name %>
  <%= game.awayTeam.name %>
<% end %>

Hope this helps :)

P.S.
This solution depends on how your models are set up of course, if you can show those it might make your problem clearer for others to explain.


EDIT - Complete solution

I will just show you the way to present a peace of info as in your solution.

To start off a small advise, you mentioned in your comment

changed it to just query the database in the form for both the homeTeamIndex and the awayTeamIndex

While this might work it is a bad practice to place such routines in the view, it is better to define a method to complete such routine in the helper and call it in the view.

Now, to the solution...

First you need to properly define model associations, which are only partially defined judging from the code you have shared. What you need is:

Tell all classes that they are related (which they are) like this:
I would loose 'Nfl' in team since it makes naming of classes confusing

class Week < ActiveRecord::Base
  has_many :games
end

class Game < ActiveRecord::Base
  belongs_to :week
  belongs_to :home_team, class_name: "Team", foreign_key: :home_team_id
  belongs_to :away_team, class_name: "Team", foreign_key: :away_team_id
end

class Team < ActiveRecord::Base
  has_many :home_games, class_name: "Game", foreign_key: :home_team_id
  has_many :away_games, class_name: "Game", foreign_key: :away_team_id
end

Note: Have a look at Active Record Associations for a more in-depth understanding of those.


EDIT

Corrected model associations above and tested to make sure those are working:)

Make sure you have the following database schema to back up the associations...

  create_table "games", force: true do |t|
    t.integer  "game_number"
    t.integer  "week_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "home_team_id"
    t.integer  "away_team_id"
  end

  create_table "teams", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "weeks", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

EDIT: mind the fact that all tables do include id field by default when you create models in rails


With these associations in place you can now get the week in your controller as you did before:

class WeeksController < ApplicationController

  ...

  def show
    @week = Week.find(params[:id])
    @games = @week.games
  end

  ...

end

And finally construct your view:
In WeeksController#show
/views/weeks/show.html.erb

<% if @games.any? %>
  <% render @games %>
<% end %>

This line "render @games' will search for '/views/games/_game.html.erb' and render it for every game in the collection @games

*/views/games/_game.html.erb - partial view*

<strong> Game <%= game.game_number %>  </strong> <br />
<%= image_tag(game.home_team.imagePath, size: "40") %>
<%= game.home_team.name %>

  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VS
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<%= image_tag(game.away_team.imagePath, size: "40") %>
<%= game.away_team.name %> <br />

And that is it, the only fiddly bit is getting the model associations wright which is not too difficult.

于 2013-09-04T19:39:29.683 に答える