Devise Custom Routes and Login Pages

送分小仙女□ 提交于 2019-11-27 06:33:50
doritostains

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' %>

The following worked for me:

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

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.

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

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

Jayaram

Use this at the top of your routes.rb file

map.connect "users/:action", :controller => 'users', :action => /[a-z]+/i 

use this on where your index file is. if it is on your users model, use the above or change accordingly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!