设计自定义路由无法找到路径的映射?

时间:2013-11-11 21:08:44

标签: ruby-on-rails devise

我正在尝试设置自定义的facebook身份验证路径,而且我一直都会遇到这个无用的设计错误。它提到我需要为路线设置我的devise_scope,正如你所看到的,我做到了!我无法弄清楚还有什么办法可以让这个错误发挥作用。

MyApp::Application.routes.draw do

root :to => 'high_voltage/pages#show', id: 'index'

namespace :api, defaults: {format: :json} do
  namespace :v1 do
    # i've removed all the extra routes for other resources. 
    # I do not have one for users set up

    devise_for :users
    devise_scope :user do
      post '/users/facebook', to: 'sessions#facebook'
    end

    get 'users/:id/events', to: 'users#events'

  end
end
end

我的帖子

myapp[development*] $ curl -H 'Content-Type: application/json' -d '{"auth_token": "12345"}' http://localhost:3000/api/v1/users/facebook

和错误

AbstractController::ActionNotFound at /api/v1/users/facebook
============================================================

> Could not find devise mapping for path "/api/v1/users/facebook".
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]



devise (3.1.1) app/controllers/devise_controller.rb, line 84
------------------------------------------------------------

``` ruby
   79       @navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] }
   80     end
   81
   82     def unknown_action!(msg)
   83       logger.debug "[Devise] #{msg}" if logger
>  84       raise AbstractController::ActionNotFound, msg
   85     end
   86
   87     # Sets the resource creating an instance variable
   88     def resource=(new_resource)
   89       instance_variable_set(:"@#{resource_name}", new_resource)
```

App backtrace
-------------



Full backtrace
--------------

 - devise (3.1.1) app/controllers/devise_controller.rb:84:in `unknown_action!'
 - devise (3.1.1) app/controllers/devise_controller.rb:59:in `assert_is_devise_resource!'
 - activesupport (4.0.0) lib/active_support/callbacks.rb:407:in `_run__4085891193320527193__process_action__callbacks'
 - activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
 - actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
 - actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
 - actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
 - activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
 - activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
 - activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
 - actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
 - actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
 - activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
 - actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
 - actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
 - actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
 - actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
 - actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
 - actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
 - actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
 - actionpack (4.0.0) lib/action_dispatch/routing/mapper.rb:44:in `call'
 - actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
 - actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
 - actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
 - meta_request (0.2.8) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
 - rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
 - meta_request (0.2.8) lib/meta_request/middlewares/headers.rb:16:in `call'
 - meta_request (0.2.8) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
 - rack-cors (0.2.8) lib/rack/cors.rb:54:in `call'
 - warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
 - warden (1.2.3) lib/warden/manager.rb:34:in `call'
 - rack (1.5.2) lib/rack/etag.rb:23:in `call'
 - rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
 - rack (1.5.2) lib/rack/head.rb:11:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
 - rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
 - rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
 - activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
 - activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
 - activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 - activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__753235512699108649__call__callbacks'
 - activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
 - actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 - better_errors (1.0.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
 - better_errors (1.0.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
 - better_errors (1.0.1) lib/better_errors/middleware.rb:56:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 - railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
 - railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
 - activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
 - activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
 - activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
 - railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
 - quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
 - actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 - rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
 - rack (1.5.2) lib/rack/runtime.rb:17:in `call'
 - activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 - rack (1.5.2) lib/rack/lock.rb:17:in `call'
 - actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
 - railties (4.0.0) lib/rails/engine.rb:511:in `call'
 - railties (4.0.0) lib/rails/application.rb:97:in `call'
 - rack (1.5.2) lib/rack/content_length.rb:14:in `call'
 - thin (1.6.1) lib/thin/connection.rb:82:in `block in pre_process'
 - thin (1.6.1) lib/thin/connection.rb:80:in `pre_process'
 - thin (1.6.1) lib/thin/connection.rb:55:in `process'
 - thin (1.6.1) lib/thin/connection.rb:41:in `receive_data'
 - eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
 - thin (1.6.1) lib/thin/backends/base.rb:73:in `start'
 - thin (1.6.1) lib/thin/server.rb:162:in `start'
 - rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
 - rack (1.5.2) lib/rack/server.rb:264:in `start'
 - railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
 - railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
 - railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
 - bin/rails:4:in `<main>'

和控制器

class Api::V1::SessionsController < Devise::SessionsController
  respond_to :json
  before_filter :authenticate_user!, :except => [:create, :destroy]
  skip_before_action :verify_authenticity_token

  def facebook
  end
end

最后我的路线..

api_v1_users_facebook POST   /api/v1/users/facebook(.:format)        api/v1/sessions#facebook {:format=>:json}

这个错误没有任何意义,因为我已经确切地说明了他们想要的方式,而且我已经尝试过各种其他解决方案,而不是在线解决方案。

谢谢!

3 个答案:

答案 0 :(得分:1)

我遇到了使用RocketPants的类似问题,但我认为它相当于。我解决了使用:path_prefix =&gt;在我的情况下'1',在你的情况下,我会尝试使用:

devise_for :users, :path_prefix => 'api/v1'

请查看此文档https://github.com/plataformatec/devise#configuring-routes

我没有将devise_for嵌套在命名空间

答案 1 :(得分:0)

我能够通过在命名空间嵌套之外移动devise_for语句来解决此错误:

Rails.application.routes.draw do

  devise_for :users, skip: :all  # Use skip: :all if you don't want all the default devise routes created

  namespace :api do
    scope module: :v1
      devise_scope :user do
        post "/sessions" => "sessions#create"
        delete "/sessions" => "sessions#destroy"
      end
    end
  end

答案 2 :(得分:0)

我的控制器的资源名称为api_v1_user,应为user。 我的解决方案是覆盖resource_name方法,例如:

class Api::V1::SessionsController < Devise::SessionsController

[...]

  protected

  def resource_name
   :user
  end

end

cf https://github.com/plataformatec/devise/issues/3663#issuecomment-169130536