自定义设计路线

时间:2014-01-22 18:19:30

标签: ruby-on-rails ruby api devise routes

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]

我尝试了很多变化,但没有用。

更新:这可能有些过分,但这是一个示例应用示例。不确定我是否做错了或者这可能是一个设计错误。

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

3 个答案:

答案 0 :(得分:2)

您错过了登录操作的 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

答案 1 :(得分:0)

顺便提一下,这个问题,OP在这个问题上有所动作: https://github.com/plataformatec/devise/issues/2840

这可以解释为什么它在2014年1月失败后于2014年8月起作用。

该主题的结束评论是:

  

这应该有助于2016年的任何人   http://stackoverflow.com/questions/22063026/how-do-i-override-devise-controller-and-devise-routes-at-the-same-time

  

任何人在做完

之后仍然面临这个问题      

devise_for :users, skip: :all devise_scope :user do end

     

如果您正在致电,请参阅此问题#2496   devise_scope在命名空间内。

答案 2 :(得分:0)

这是我如何解决此问题的示例:

Rails.application.routes.draw do

  # This is the fix.
  devise_for :users, skip: :all

  devise_scope :user do
    get "/users/sign_in", to: "devise/sessions#new", as: :new_user_session
    post "/users/sign_in", to: "devise/sessions#create", as: :user_session
    delete "/users/sign_out", to: "devise/sessions#destroy", as: :destroy_user_session
    get "/users/password/new", to: "devise/passwords#new", as: :new_user_password
    get "/users/password/edit", to: "devise/passwords#edit", as: :edit_user_password

    patch "/users/password", to: "devise/passwords#update", as: :user_password
    put "/users/password", to: "devise/passwords#update"
    post "/users/password", to: "devise/passwords#create"

    get "/users/cancel", to: "devise/registrations#cancel", as: :cancel_user_registration
    get "/users/sign_up", to: "devise/registrations#new", as: :new_user_registration
    get "/users/edit", to: "devise/registrations#edit", as: :edit_user_registration

    patch "/users", to: "devise/registrations#update", as: :user_registration
    put "/users", to: "devise/registrations#update"
    delete "/users", to: "devise/registrations#destroy"
    post "/users", to: "devise/registrations#create"
  end
end

GuyPaddock,谢谢您的提示。想通了,我会将其作为单独的答案发布,因为您的答案没有完整的代码示例,也没有明确声明这是一种解决方案。

相关问题