Calvin
Calvin

Reputation: 3312

Rails controller params undefined

I am trying to make a simple api on Rails. (v4.1.8) However, it seems like the parameters that I send never reach the controller. I always get:

undefined method `parameters' for nil:NilClass

The controller code is:

class API::V1::ServiceController < ApiBaseController
  def initialize
    puts 'OK. I am here.' # <---- I can get here without error.
    puts params.inspect # <---- Here is where the error occur.
    render json: {message: 'Test completed.'}
  end
end

ApiBaseController is nothing:

class ApiBaseController < ActionController::Base

end

Route:

namespace :api , :defaults => {:format => :json} do
  namespace :v1 do
    get 'initialize',       to: 'service#initialize'
  end
end

My test URL:

http://localhost:3000/api/v1/initialize?param1=one

Upvotes: 1

Views: 2658

Answers (1)

Santhosh
Santhosh

Reputation: 29124

This error is due to the use of initialize as an action name. In Ruby, initialize is a keyword, denoting the class constructor. In this case, params is getting called before the controller class is initialized, and hence the error.

Refer this for more information about the initialize keyword, and this list of all ruby keywords.

Upvotes: 6

Related Questions