设计中的路由问题[无法找到设计映射..]

时间:2015-12-05 09:44:46

标签: devise

我和Devise一起度过了一段艰难的时光,因为它一直让我犯错误

[Devise] Could not find devise mapping for path "/api/v1/user/login.json".
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]

此处没有进一步说明routes.rb看起来像

  namespace :api do 
    namespace :v1 do
      namespace :user do
        devise_for :users,skip: :all
        devise_scope :user do
          post '/login' => 'sessions#create'
        end
      end
    end
  end

rake routes =>

api_v1_user_login POST /api/v1/user/login(.:format) api/v1/user/sessions#create

以及目录结构的外观

app/controllers/
├── api
│   └── v1
│       ├── sessions_controller.rb
│       └── user
│           └── sessions_controller.rb

cat app/controllers/api/v1/user/sessions_controller.rb

class Api::V1::User::SessionsController < Devise::SessionsController
  #Api::V1::SessionsController
  skip_before_filter :authenticate_user_from_token

  def create
    binding.pry
    authenticate_resource = warden.authenticate(:scope => resource_name)
    if auth_res
      render :status => 200,
             :json => { :request_status => :success,
                        :success => true,
                        :info => "Logged in",
                        :data => { :auth_token => current_user.authentication_token } }
    else
      render :status => 401,
             :json => { :request_status => :error,
                        :success => false,
                        :errors => ["Invalid email or password"],
                        :status => :invalid }
    end
  end  
end

我在这里失踪的任何线索。

1 个答案:

答案 0 :(得分:0)

修正了它

在[source]

之前定义devise_for

https://github.com/plataformatec/devise/issues/2840

devise_for :users,skip: :all

namespace :api do 
    namespace :v1 do
      namespace :user do
        devise_scope :user do
          post '/login' => 'sessions#create'
        end
      end
    end
  end