Devise Custom Routes and Login Pages

54,026

Solution 1

With Devise 1.1.3 the following should work

devise_for :user, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

The routes it creates will not be appended with "/user/..." because of the :path parameter being an empty string. The :pathnames hash will take care of naming the routes as you like. Devise will use these routes internally so submitting to /login will work as you wish and not take you to /user/log_in

To add a login form to your front page there's info at the Devise Wiki: http://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

Or do something like this:

 <%= form_tag new_user_session_path do %>
  <%= text_field_tag 'user[email]' %>
  <%= password_field_tag 'user[password]' %>
 <%=  submit_tag 'Login' %>

Solution 2

The following worked for me:

  devise_for :users do
    get "/login" => "devise/sessions#new"
    get "/register" => "devise/registrations#new"
  end

Solution 3

Config:

  devise_scope :user do
    get 'profile/edit'    => 'devise/registrations#edit',   :as => :edit_user_registration
    get 'profile/cancel'  => 'devise/registrations#cancel', :as => :cancel_user_registration
  end

  devise_for  :users,
              :path => '',
              :path_names => {  :sign_in =>       'login',
                                :sign_out =>      'logout',
                                :sign_up =>       '',
                                :registration =>  'register',
                                :edit =>          'edit',
                                :cancel =>        'cancel',
                                :confirmation =>  'verification'  }

Routes:

  edit_user_registration GET    /profile/edit(.:format)      devise/registrations#edit
cancel_user_registration GET    /profile/cancel(.:format)    devise/registrations#cancel
        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   /password(.:format)          devise/passwords#create
       new_user_password GET    /password/new(.:format)      devise/passwords#new
      edit_user_password GET    /password/edit(.:format)     devise/passwords#edit
                         PATCH  /password(.:format)          devise/passwords#update
                         PUT    /password(.:format)          devise/passwords#update
                         GET    /register/cancel(.:format)   registrations#cancel
       user_registration POST   /register(.:format)          registrations#create
   new_user_registration GET    /register(.:format)          registrations#new
                         GET    /register/edit(.:format)     registrations#edit
                         PATCH  /register(.:format)          registrations#update
                         PUT    /register(.:format)          registrations#update
                         DELETE /register(.:format)          registrations#destroy

Solution 4

You just need don't put your special route in devise_for block

match '/dashboard' => 'home#dashboard', :as => 'user_root'
get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
devise_for :user

Now /login works. /users/sign_in too.

Solution 5

I created my own auth controller and routed devise sessions controller to my controller

devise_for :users, 
:controllers => {
    :sessions => 'auth' },

:path => '/',

:path_names => {
    :sign_in  => 'login',
    :sign_out => 'logout' }

This code will add /login and /logout urls.

More about this you can find in source code http://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb

Share:
54,026
Karthik Kastury
Author by

Karthik Kastury

A 24x7 Internet Geek. You can find me on Twitter(@KarthikDot) or else you also have an option to find me on /dev/null

Updated on July 09, 2022

Comments

  • Karthik Kastury
    Karthik Kastury almost 2 years

    I'm trying to get Custom Routes working in my Rails application (Ruby 1.9.2 with Rails 3).

    This is my config/routes.rb file

    match '/dashboard' => 'home#dashboard', :as => 'user_root'
    devise_for :user do
       get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
       get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
       get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
    end
    

    But submitting the form on /login or /register goes to users/sign_in and users/sign_up. How do I prevent this from happening. Or even better make sure that by default all requests for users/sign_in etc go to the relevant routes and not the default routes generated by Devise.

    Also how can I make the login form a partial to include it in any controller? So that I can have the Login Page on the homepage (home#index) and not on users/sign_in?

    I'm using Devise 1.1.3 with Rails 3 on Ruby 1.9.2, on Mac OSX Snow Leopard.

    Thanks!

  • Karthik Kastury
    Karthik Kastury over 13 years
    Will this work for having the login pages on the home page. They reside in the home#index. And devise (github.com/plataformatec/devise) uses the user model.
  • Karthik Kastury
    Karthik Kastury over 13 years
    I don't want the users/sign_in route to work. I only want the custom routes to work, and they should be active in all the controllers and views that use it.
  • Karthik Kastury
    Karthik Kastury over 13 years
    Doesn't work. My controller's name is home and it doesn't detect the route / on the home page.
  • Karthik Kastury
    Karthik Kastury over 13 years
    Canthiswait's solution works awesome, so I didn't try creating a new custom controller.
  • Vlada
    Vlada over 13 years
    gr8 :) of course you don't need controller setting :path_names is crucial
  • tokland
    tokland over 11 years
    In the code you put ":path", in the text you said ":as", fixed.
  • AJcodez
    AJcodez over 11 years
    notice Devise 2.1.2 (later version) uses the plural devise_for :users
  • Abe Voelker
    Abe Voelker over 9 years
    This approach works, but you are stuck using the default Devise path helpers like new_user_session_path. To get helpers like login_path, you can put the devise_for :user do block that OP has right below this answer, and you'll get the best of both worlds.