【问题标题】:Rails: parameters visible in console but not accessible as paramsRails:参数在控制台中可见但不能作为参数访问
【发布时间】:2016-11-23 21:26:10
【问题描述】:

我正在向 url 发送一个 get 请求:groups/:id.json

在我的 Rails 服务器控制台中,我看到以下内容:

Processing by GroupsController#show as JSON
  Parameters: {"id"=>"11"}

但我无法访问此 show 控制器方法中的 params 对象。以下:

puts "puts params!"
puts params

不向控制台输入任何内容:

puts params!
#<ActionController::Parameters:0x007f7f9c482bf0>
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms) 

NoMethodError (undefined method `id' for #<ActionController::Parameters:0x007f7f9c482bf0>):

我的 rails 项目用作 angular ui-router 前端的 API,我正在使用 Devise 进行用户管理。什么可能会阻止我的参数出现在此方法中?

【问题讨论】:

    标签: ruby-on-rails angularjs devise


    【解决方案1】:

    您是否将id 作为参数上的方法调用?应该是params[:id] 而不是params.id

    【讨论】:

      【解决方案2】:

      这是您尝试在控制器操作中访问paramters 的方式。应该是params[:id]Take a look at the docs, 'Parameters' section.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-06
        • 2017-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 1970-01-01
        相关资源
        最近更新 更多