任何人都可以提供幫助嗎?在此先感謝您的回覆。NoMethodError(未定義的方法`id'for nil:NilClass):in Rails 4.0.2
在應用程序/控制器/ heats_controller.rb,代碼如下:
class HeatsController < ApplicationController
def add_car
Pusher[params[:sendTo]].trigger('addCar', car_params)
head :ok
end
def index
@heats = Heat.all
render json: @heats
end
def new
race = Race.all.sample
render json: { start_time: Time.now.to_s,
race_id: race.id,
text: race.passage }
end
def show
@heat = Heat.find(params[:id])
end
def start_game
Pusher[params[:sendTo]].trigger('initiateCountDown', start_heat_params)
head :ok
end
def update_board
Pusher[params[:channel]].trigger('updateBoard', car_params)
head :ok
end
private
def heat_params
params.require(:heat).permit(:race_id)
end
def car_params
params.permit(:racer_id,
:racer_name,
:return_to,
:progress,
:racer_img,
:wpm,
:channel,
:sendTo)
end
def start_heat_params
params.permit(:channel, :race_id, :text, :timer)
end
end
在應用程序/模型/ heat.rb,代碼如下:
class Heat < ActiveRecord::Base
belongs_to :race
has_many :racer_stats
end
任何幫助可以理解,謝謝
錯誤:
Processing by HeatsController#new as */*
Completed 500 Internal Server Error in 6ms
NoMethodError (undefined method `id' for nil:NilClass):
app/controllers/heats_controller.rb:18:in `new'
在問題中添加導軌服務器日誌.. –
Race.all.sample中的'sample'是什麼? – RSB