[英]Rails controller params undefined
I am trying to make a simple api on Rails. 我正在尝试在Rails上制作一个简单的api。 (v4.1.8) However, it seems like the parameters that I send never reach the controller. (v4.1.8)但是,似乎我发送的参数从未到达控制器。 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: ApiBaseController没什么:
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
This error is due to the use of initialize
as an action name. 此错误是由于使用initialize
作为操作名称引起的。 In Ruby, initialize
is a keyword, denoting the class constructor. 在Ruby中, initialize
是一个关键字,表示类的构造函数。 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 . 请参阅此以获取有关initialize
关键字以及所有ruby关键字的列表的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.