私人方法`要求&#39;呼叫#<hash:0x0000000645bdb0>

时间:2015-09-25 15:32:15

标签: ruby-on-rails ruby api ruby-on-rails-4 strong-parameters

我尝试通过将emailpassword作为参数传递给/ api / v1 / sign_in来验证Rails v.4.2.2应用程序,从而尝试通过API进行身份验证。我使用邮递员提出请求。

这是我将请求发送到服务器时收到的错误:

Started POST "/api/v1/users/sign_in" for 127.0.0.1 at 2015-09-25 20:46:31 +0530
Processing by Api::V1::SessionsController#create as */*
  Parameters: {"user"=>{"email"=>"example@example.com", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

NoMethodError (private method `require' called for #<Hash:0x0000000645bdb0>):
  app/controllers/api/v1/sessions_controller.rb:18:in `user_params'
  app/controllers/api/v1/sessions_controller.rb:22:in `ensure_params_exist'


  Rendered /home/tigon/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.5ms)
  Rendered /home/tigon/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.9ms)
  Rendered /home/tigon/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /home/tigon/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (26.5ms)

我在app / controllers / api / v1 / sessions_controller.rb的代码中从params.require收到错误:

class Api::V1::SessionsController < Api::V1::BaseController
  skip_before_action :authenticate_user_from_token!
  before_action :ensure_params_exist
  respond_to :json

  def create
    @user = User.find_for_database_authentication(email: user_params[:email])
    return invalid_login_attempt unless @user
    return invalid_login_attempt unless @user.valid_password?(user_params[:password])
    @auth_token = jwt_token(@user)
  end

  private

  def user_params
    params.require(:user).permit(:email, :password)
  end

  def ensure_params_exist
    if user_params[:email].blank? || user_params[:password].blank?
      return render_unauthorized errors: { unauthenticated: ["Incomplete credentials"] }
    end
  end

  def invalid_login_attempt
    render_unauthorized errors: { unauthenticated: ["Invalid credentials"] }
  end
end

对于raise params.inspect,我收到以下回复:

{"------WebKitFormBoundaryJogEDEbfqWmnfhSL\r\nContent-Disposition: form-data; name=\"user[email]\"\r\n\r\example@example.com\r\n------WebKitFormBoundaryJogEDEbfqWmnfhSL\r\nContent-Disposition: form-data; name=\"user[password]\"\r\n\r\password\r\n------WebKitFormBoundaryJogEDEbfqWmnfhSL--\r\n"=>nil}

我被params.require卡住了好几个小时,我无法弄清楚确切的问题。有谁知道这个问题?

>rake middleware
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000006d11c70>
use Rack::Runtime
use ActionDispatch::RequestId
use RequestStore::Middleware
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Warden::Manager
run MyApp::Application.routes

0 个答案:

没有答案