ruby - NoMethodError (undefined method `id' for nil:NilClass): in Rails 4.0.2 -


can this?thanks in advance reply.

in app/controllers/heats_controller.rb, code below:

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 

in app/models/heat.rb, code below:

class heat < activerecord::base   belongs_to :race   has_many :racer_stats end 

any appreciated ,thank you


error:

processing heatscontroller#new */* completed 500 internal server error in 6ms  nomethoderror (undefined method `id' nil:nilclass):   app/controllers/heats_controller.rb:18:in `new' 

it seems race.all.sample not work.

maybe have no records in races table @ all.

also, try retrieve random record (for rails 4):

def new    offset = rand(race.count)    race.offset(offset).first    ... end 

Comments