ruby-on-rails – 自定义设计路线

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

所以我想我会根据文档创建自己的路线.所以我改变了我的路线.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]

我尝试了很多变化,但无济于事.

更新:这可能是矫枉过正,但这是一个示例应用程序示例.不确定我是否做错了或者这可能是一个设计错误.

示例应用程序:https://github.com/michaelirey/devise_weird

最佳答案 你错过了登录操作的GET,它显示了登录表单..你只有会话创建,修复它就像这样(你:

devise_scope :user do
  get   '/users/sign_in'  => 'custom_devise/sessions#new',  as: :new_user_session      
  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
点赞