vendredi 29 janvier 2016

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

Can anyone help with this?Thanks in advance for your reply.

In app/controllers/heats_controller.rb, the code is as 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, the code is as below:

class Heat < ActiveRecord::Base
  belongs_to :race
  has_many :racer_stats
end

Any help will be appreciated ,thank you

Aucun commentaire:

Enregistrer un commentaire