How to change the login and signup urls in devise plugin Rails

前端 未结 6 2311
失恋的感觉
失恋的感觉 2020-12-13 02:29

I am using devise plugin in my new Rails App. My issue is devise plugin has default roots for login and signup

/users/sign_in
/users/sign_up
<
相关标签:
6条回答
  • 2020-12-13 03:08

    Here are a little bit more options than you asked but it's clear:

      devise_for :users,
                 :controllers => { :registrations => "users/registrations",
                                   :confirmations => "users/confirmations",
                                   :sessions => 'devise/sessions'},
                 :skip => [:sessions] do
        get '/signin'   => "devise/sessions#new",       :as => :new_user_session
        post '/signin'  => 'devise/sessions#create',    :as => :user_session
        get '/signout'  => 'devise/sessions#destroy',   :as => :destroy_user_session
        get "/signup"   => "users/registrations#new",   :as => :new_user_registration
      end
    

    Even more, with :registrations => "users/registrations" we can additionally customize redirects:

    class Users::RegistrationsController < Devise::RegistrationsController
      protected
    
      def after_sign_up_path_for(resource)
        welcome_path # it's not a home path
      end
    
      def after_update_path_for(resource)
        edit_user_registration_path
      end
    end
    

    Devise has a good wiki.

    0 讨论(0)
  • 2020-12-13 03:12

    As this may still be the n°1 result people get when looking for that question, it might be useful to note that there is now a simple way to do that:

    devise_for :users, path: '', path_names: { sign_in: 'login', sign_up: 'signup'}
    

    Reference: https://github.com/heartcombo/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

    0 讨论(0)
  • 2020-12-13 03:14

    If I am reading and understanding your question correctly, you are after 'match'.

    devise_for :users
    match "/login" => "devise/sessions#new"
    match "/signup" => "devise/registrations#new"

    Be sure to have them in the correct order as they are matched based on line numbers in the file.

    More can be found at: http://guides.rubyonrails.org/routing.html

    0 讨论(0)
  • 2020-12-13 03:20

    i hope i'm not too late ;)

    here is how it works (just paste it in your routes.rb and you good to go):

      devise_for :users, :controllers => {:sessions => 'devise/sessions'}, :skip => [:sessions] do
        get 'login' => 'devise/sessions#new', :as => :new_user_session
        post 'login' => 'devise/sessions#create', :as => :user_session
        get 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
        get 'register' => 'devise/registrations#new', :as => :new_user_registration
      end
    
    0 讨论(0)
  • 2020-12-13 03:25

    I was able to fix my issue by using the following code in my routes

      devise_for :users,
               :controllers => { :sessions => 'devise/sessions'},
               :skip => [:sessions] do
        get '/login'   => "devise/sessions#new",       :as => :new_user_session
        post '/login'  => 'devise/sessions#create',    :as => :user_session
        get '/signout'  => 'devise/sessions#destroy',   :as => :destroy_user_session
        get "/signup" => "devise/registrations#new", :as => :new_user_registration
      end
    

    But still in my views if I use

      link_to "Register", new_user_registration_path
    

    In my browser its showing as

      /user/sign_up   and not as /signup
    

    But if I directly type /signup I will get the registraion page. Is there any mapping I need to do here.

    0 讨论(0)
  • 2020-12-13 03:29

    edit config/routes.rb

    devise_for :users, path: ''

     devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }
    
         new_user_session GET    /login(.:format)                         devise/sessions#new
                    user_session POST   /login(.:format)                         devise/sessions#create
            destroy_user_session DELETE /logout(.:format)                        devise/sessions#destroy
                   user_password POST   /secret(.:format)                        devise/passwords#create
               new_user_password GET    /secret/new(.:format)                    devise/passwords#new
              edit_user_password GET    /secret/edit(.:format)                   devise/passwords#edit
                                 PATCH  /secret(.:format)                        devise/passwords#update
                                 PUT    /secret(.:format)                        devise/passwords#update
        cancel_user_registration GET    /register/cancel(.:format)               devise/registrations#cancel
               user_registration POST   /register(.:format)                      devise/registrations#create
           new_user_registration GET    /register/cmon_let_me_in(.:format)       devise/registrations#new
          edit_user_registration GET    /register/edit(.:format)                 devise/registrations#edit
                                 PATCH  /register(.:format)                      devise/registrations#update
                                 PUT    /register(.:format)                      devise/registrations#update
                                 DELETE /register(.:format)                      devise/registrations#destroy
    
    0 讨论(0)
提交回复
热议问题