【发布时间】:2014-01-22 18:19:29
【问题描述】:
Using devise (3.2.2)
Using rails (4.0.2)
致力于开发只需要某些路由的 API。我的routes.rb 中有以下内容:
devise_for :users, path: '/users', controllers: {
sessions: 'v1/custom_devise/sessions',
passwords: 'v1/custom_devise/passwords'
}
太棒了!现在我们有了所有这些路线:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) v1/custom_devise/sessions#new
user_session POST /users/sign_in(.:format) v1/custom_devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) v1/custom_devise/sessions#destroy
user_password POST /users/password(.:format) v1/custom_devise/passwords#create
new_user_password GET /users/password/new(.:format) v1/custom_devise/passwords#new
edit_user_password GET /users/password/edit(.:format) v1/custom_devise/passwords#edit
PATCH /users/password(.:format) v1/custom_devise/passwords#update
PUT /users/password(.:format) v1/custom_devise/passwords#update
但我不想要或不需要这些路线:
new_user_session GET /users/sign_in(.:format) v1/custom_devise/sessions#new
new_user_password GET /users/password/new(.:format) v1/custom_devise/passwords#new
edit_user_password GET /users/password/edit(.:format) v1/custom_devise/passwords#edit
所以我想我会按照文档创建自己的路线。于是我换了routes.rb:
devise_scope :user do
post '/users/sign_in' => 'custom_devise/sessions#create', as: :user_session
delete '/users/sign_out' => 'custom_devise/sessions#destroy', as: :destroy_user_session
post '/users/password' => 'custom_devise/passwords#create', as: :user_password
put '/users/password' => 'custom_devise/passwords#update', as: nil
patch '/users/password' => 'custom_devise/passwords#update', as: nil
end
现在我的路线看起来很完美,就像我想要的那样:
Prefix Verb URI Pattern Controller#Action
user_session POST /users/sign_in(.:format) v1/custom_devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) v1/custom_devise/sessions#destroy
user_password POST /users/password(.:format) v1/custom_devise/passwords#create
PUT /users/password(.:format) v1/custom_devise/passwords#update
PATCH /users/password(.:format) v1/custom_devise/passwords#update
但现在我的请求失败了:
AbstractController::ActionNotFound:
Could not find devise mapping for path "/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/sign_in"用于设计新会话,因此您将需要new_user_session路由。 -
@CharlesJHardy:不需要对
/users/sign_in的GET请求,因为这是一个API。 -
这些故障是发生在测试中还是在开发中?
-
@LouisSimoneau:发展
-
我克隆了示例应用,它运行良好。
标签: ruby-on-rails ruby api devise routes