2017-03-14 12 views
-1

我的routes.rb -API命中誤差給出 - 完成500內部服務器錯誤,類型錯誤(未繳的隱式轉換成字符串)

Rails.application.routes.draw do 
    root 'welcome#index' 

    namespace :api, defaults: { format: :json } do 
    namespace :v1 do 
     get "https://stackoverflow.com/users/:cv_id" => "engine#users_get" 
     post "/users" => "engine#users_add" 
    end 
    end 

和我的主控制器是 -

class Api::V1::MainController < ApplicationController 

    http_basic_authenticate_with name: ENV["API_USERNAME"], password: ENV["API_PASSWORD"] 

    API_CMDS_LIST = { 
    "Engine" => [:users_add, :users_update], 
    } 

    include Api::V1::ErrorMessage 
    skip_before_filter :verify_authenticity_token 
    protect_from_forgery with: :null_session 

    def resulting_json(resp) 
    if resp[:result] == "error" && !resp[:code] 
     resp[:code] = Api::V1::ErrorMessage::CODES[resp[:message]] || Api::V1::ErrorMessage::DEFAULT_CODE 
    end 

    render json: resp 
    end 

    def authenticate 
    #authenticate_or_request_with_http_basic do |user, password| 

     #File.write('test.txt', "USER: #{user}. PASS: #{password}") 
     #@user && password == @user.secret_token #&& @user.belongs_to_active_brand? 
     true 
    #end 
    end 

    def validate_request 
    action = params[:action] 
    klass = API_CMDS_LIST.select{ |klass, methods| methods.include?(action.to_sym) }.keys[0] 

    @req = "Api::V1::#{klass}".constantize.new(send("# {action}_params").merge({user: @user}), request) 

    if [email protected]? 
     err_msg = @req.errors.full_messages.first 
     code = Api::V1::ErrorMessage::CODES[err_msg] || Api::V1::ErrorMessage::DEFAULT_CODE 
     render json: { result: "error", msg: err_msg, code: code } and return 
    end 
    end 

end 

和發動機控制器代碼 -

class Api::V1::EngineController < Api::V1::MainController 
    include Api::V1::ErrorMessage 
    before_filter :validate_request 

    API_CMDS_LIST["Engine"].each do |method| 

    define_method(method) do 
     response = @req.send(method) 
     resulting_json(response) 
    end 
    end 

    def users_add_params 
    params.permit(:first_name, :last_name) 
    end 
end 

,但是當我嘗試打端點「/用戶」使用參數{FIRST_NAME:「SATY am「,last_name:」agrawal「}它給我這個錯誤---

Api :: V1 :: EngineController#users_add處理爲JSON 參數:{」first_name「=>」satyam「,」last_name「 => 「阿格拉瓦爾」} 完成500內部服務器錯誤在1ms內(ActiveRecord的:0.0ms)

類型錯誤(的零的隱式轉換成字符串):

的ActiveSupport(5.0.1)LIB/active_support/security_utils .rb:23:digest' activesupport (5.0.1) lib/active_support/security_utils.rb:23:in十六進制' activesupport(5.0.1)lib/active_support/security_utils.rb:23:在variable_size_secure_compare' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:75:in塊中(2級)在http_basic_authenticate_與 ' ActionPack的(5.0.1)LIB/action_controller /金屬/ http_authentication.rb:97:在call' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:97:in的authenticate' ActionPack的(5.0.1)LIB/action_controller /金屬/ http_authentication.rb:87:在authenticate_with_http_basic' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:83:in authenticate_or_request_with_http_basic」 ActionPack的(5.0.1)lib/action_controller/metal/http_authentication.rb:71:block in http_basic_authenticate_with' activesupport (5.0.1) lib/active_support/callbacks.rb:396:in instance_exec' activesupport(5.0.1)lib/active_support/callbacks.rb:396:在block in make_lambda' activesupport (5.0.1) lib/active_support/callbacks.rb:169:in調用' activesupport(5.0.1)lib /active_support/callbacks.rb:169:in block (2 levels) in halting' actionpack (5.0.1) lib/abstract_controller/callbacks.rb:12:in調用' actionpack(5.0.1)lib/abstract_controller/callbacks.rb:12:在block (2 levels) in <module:Callbacks>' activesupport (5.0.1) lib/active_support/callbacks.rb:170:in調用' activesupport(5.0.1)lib/active_support/callbacks.rb:170 :在block in halting' activesupport (5.0.1) lib/active_support/callbacks.rb:454:in調用' 的ActiveSupport(5.0.1)LIB/active_support/callbacks.rb:454:在block in call' activesupport (5.0.1) lib/active_support/callbacks.rb:454:in每個 ' 的ActiveSupport(5.0.1)LIB/active_support/callbacks.rb:454:在call' activesupport (5.0.1) lib/active_support/callbacks.rb:101:inrun_callbacks' 的ActiveSupport(5.0.1 )lib/active_support/callbacks.rb:750:in _run_process_action_callbacks' activesupport (5.0.1) lib/active_support/callbacks.rb:90:in run_callbacks' actionpack(5.0.1)lib/abstract_controller/callbacks.rb:19:in process_action' actionpack (5.0.1) lib/action_controller/metal/rescue.rb:20:in process_action' actionpack(5.0.1)lib/action_controller/metal/instrumentation .rb:32:在block in process_action' activesupport (5.0.1) lib/active_support/notifications.rb:164:in塊中的儀器' activesupport(5.0.1)lib/active_support/notifications/instrumenter.rb:21:在instrument' activesupport (5.0.1) lib/active_support/notifications.rb:164:in儀器' actionpack(5.0.1)lib/action_controller/metal/instrumentation.rb :30:在process_action' actionpack (5.0.1) lib/action_controller/metal/params_wrapper.rb:248:in process_action ' 了activerecord(5.0.1)LIB/active_record/railties/controller_runtime.rb:18:在process_action' actionpack (5.0.1) lib/abstract_controller/base.rb:126:in過程 的ActionView(5.0.1)LIB/ACTION_VIEW/rendering.rb:30:在process' actionpack (5.0.1) lib/action_controller/metal.rb:190:in調度' ActionPack的( 5.0.1)lib/action_controller/metal.rb:262:在dispatch' actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:50:in發送' actionpack(5.0.1)lib/action_dispatch/routing/route_set.rb:32:在serve' actionpack (5.0.1) lib/action_dispatch/journey/router.rb:39:in塊中發送' actionpack(5.0.1) lib/action_dispatch/journey/router.rb:26:在each' actionpack (5.0.1) lib/action_dispatch/journey/router.rb:26:in服務' actionpack(5.0.1)lib/action_dispatch/routing/route_set.rb:725:在call' rack (2.0.1) lib/rack/etag.rb:25:in中調用' rack(2.0.1)lib/rack/conditional_get。rb:38:在call' rack (2.0.1) lib/rack/head.rb:12:in調用' rack(2.0.1)lib/rack/session/abstract/id.rb:222:在context' rack (2.0.1) lib/rack/session/abstract/id.rb:216:in調用' actionpack(5.0.1)lib/action_dispatch/middleware/cookies.rb: 613:在call' activerecord (5.0.1) lib/active_record/migration.rb:553:in呼叫 ' ActionPack的(5.0.1)LIB/action_dispatch /中間件/ callbacks.rb:38:在block in call' activesupport (5.0.1) lib/active_support/callbacks.rb:97:inrun_callbacks' 的ActiveSupport(5.0.1)LIB/active_support/callbacks.rb:750:在_run_call_callbacks' activesupport (5.0.1) lib/active_support/callbacks.rb:90:in run_callbacks' ActionPack的(5.0.1)LIB/action_dispatch /中間件/ callbacks.rb:36:在call' actionpack (5.0.1) lib/action_dispatch/middleware/executor.rb:12:in呼叫 ' ActionPack的(5.0.1)LIB/action_dispatch /中間件/ remote_ip.rb:79:在call' actionpack (5.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in呼叫' 幅-console(3.4.0)lib/web_console/middleware.rb:135:在012中block in call' web-console(3.4.0)lib/web_console/middleware.rb:18:in catch' web-console (3.4.0) lib/web_console/middleware.rb:18:in call' actionpack(5.0.1)lib/action_dispatch/middleware/show_exceptions.rb:31:in call' railties (5.0.1) lib/rails/rack/logger.rb:36:in call_app ' railties(5.0.1)lib/rails/rack/logger.rb:24:block in call' activesupport (5.0.1) lib/active_support/tagged_logging.rb:69:in標記爲' activesupport(5.0.1)lib/active_support/tagged_logging.rb:26:tagged' activesupport (5.0.1) lib/active_support/tagged_logging.rb:69:in' railties( 5.0.1)lib/rails/rack/logger.rb:24:call' sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in調用' actionpack(5.0.1)lib/action_dispatch/middleware/request_id.rb:24:在call' rack (2.0.1) lib/rack/method_override.rb:22:in調用' rack(2.0.1) lib/rack/runtime.rb:22:在call' activesupport (5.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in中調用' actionpack(5.0.1)lib/action_disp atch/middleware/executor.rb:12:在call' actionpack (5.0.1) lib/action_dispatch/middleware/static.rb:136:in調用' rack(2.0.1)lib/rack/sendfile.rb:111:在call' railties (5.0.1) lib/rails/engine.rb:522:in調用' puma(3.7.1)lib/puma/configuration.rb: 232:在call' puma (3.7.1) lib/puma/server.rb:578:in handle_request' puma(3.7.1)lib/puma/server.rb:415:在process_client' puma (3.7.1) lib/puma/server.rb:275:in塊中運行' puma(3.7.1)lib/puma/thread_pool.rb:120:in call' puma (3.7.1) lib/puma/thread_pool.rb:120:in block in spawn_thread」

請幫我整理出來

+1

您尋求幫助並粘貼了您希望有人花時間閱讀的所有代碼,您甚至無法花時間正確格式化它?再想想你如何尋求幫助。 – margo

回答

1

有了你給它看起來像它的失敗上main_controller.rb日誌

http_basic_authenticate_with name: ENV["API_USERNAME"], password: ENV["API_PASSWORD"] 

調試的一種方法是進入您的rails控制檯並檢查是否設置了您的環境變量。

rails console 

ENV['API_USERNAME'] 
    => 'api_username' 

ENV['API_PASSWORD'] 
    => 'hash1234090909' 
+0

謝謝!大衛它的作品 –

相關問題