更改devise_token_auth sign_in的URL

时间:2016-06-29 10:31:14

标签: ruby-on-rails devise routing

我使用devise_token_auth并尝试更改用户sign_in网址。我在api/v2/文件夹和 devise_token_auth 路由中有sessions_controller,如下所示:

路线:

namespace :api do
    namespace :v2 do
        mount_devise_token_auth_for 'User', at: 'users', :controllers => {
          :registrations => 'api/v2/registrations',
          :confirmations => 'api/v2/confirmations',
          :passwords => 'api/v2/passwords',
          :sessions => 'api/v2/sessions'}

        post 'users/sign-in', to: 'sessions#create'
    end
end

当我rake routes时,我会按照自己的意愿获得路线

api_v2_users_sign_in POST /api/v2/users/sign-in(.:format) api/v2/sessions#create

但是,当我尝试点击网址时,我收到以下错误:

AbstractController::ActionNotFound (Could not find devise mapping for path "/api/v2/users/sign-in".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    get "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]

):

/Users/gokul/.rvm/gems/ruby-2.3.0@rails5.0/bundler/gems/devise-bcdd54cc5ebd/app/controllers/devise_controller.rb:88:in `unknown_action!'
/Users/gokul/.rvm/gems/ruby-2.3.0@rails5.0/bundler/gems/devise-bcdd54cc5ebd/app/controllers/devise_controller.rb:63:in `assert_is_devise_resource!'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:382:in `block in make_lambda'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:169:in `block (2 levels) in halting'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:12:in `block (2 levels) in <module:Callbacks>'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:170:in `block in halting'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:454:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:454:in `each'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:454:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/rescue.rb:31:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.rc1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.rc1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:126:in `process'
actionpack (5.0.0.rc1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.rc1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:725:in `call'
apipie-rails (0.3.6) lib/apipie/static_dispatcher.rb:65:in `call'
actionview (5.0.0.rc1) lib/action_view/digestor.rb:12:in `call'
apipie-rails (0.3.6) lib/apipie/extractor/recorder.rb:132:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.0.rc1) lib/rack/etag.rb:25:in `call'
rack (2.0.0.rc1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.0.rc1) lib/rack/head.rb:12:in `call'
activerecord (5.0.0.rc1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.0.rc1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.rc1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.0.rc1) lib/rack/sendfile.rb:111:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
railties (5.0.0.rc1) lib/rails/engine.rb:522:in `call'
puma (3.2.0) lib/puma/configuration.rb:227:in `call'
puma (3.2.0) lib/puma/server.rb:561:in `handle_request'
puma (3.2.0) lib/puma/server.rb:406:in `process_client'
puma (3.2.0) lib/puma/server.rb:271:in `block in run'
puma (3.2.0) lib/puma/thread_pool.rb:111:in `block in spawn_thread' 

我需要网址为api/v2/users/sign-in。请帮帮我。

谢谢..

1 个答案:

答案 0 :(得分:1)

我通过在路线中向用户添加默认范围来解决这个问题,如下所示:

      mount_devise_token_auth_for 'User', at: 'users', :controllers => {
          :registrations => 'api/v2/registrations',
          :confirmations => 'api/v2/confirmations',
          :passwords => 'api/v2/passwords'
      }, skip: [:sessions]

      devise_scope :api_v2_user do
        get 'users/sign-in', to: 'sessions#new'
        post 'users/sign-in', to: 'sessions#create'
        delete 'users/sign-out', to: 'sessions#destroy'
      end

如果您的API是命名空间,那么使用命名空间devise_scope ..

非常重要

从这个链接得到答案:https://stackoverflow.com/a/33503944/4172728