routes.rb 裏有git
<!-- lang: ruby --> devise_for :users, path: '', path_names: { sign_in: :login, sign_out: :logout, sign_up: :sign_up }, controllers: { omniauth_callbacks: :omniauth_callbacks, passwords: :passwords }
一行routes.rb裏的代碼github
<!-- lang: ruby --> devise_for :users
意味着,你能夠有如下連接ruby
<!-- lang: ruby --> # Session routes for Authenticatable (default) new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"} user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"} destroy_user_session DELETE /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"} # Password routes for Recoverable, if User model has :recoverable configured new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"} edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"} user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"} POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"} # Confirmation routes for Confirmable, if User model has :confirmable configured new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"} user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"} POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
其中 devise 做用爲 namespace, 而controller有 sessions, passwords, confirmations, 後面天然就是各個 actions 了...session
相比默認的 devise_for 說說咱們用到的參數:app
- :path => allows you to setup path name that will be used, as rails routes does. The following route configuration would setup your route as /accounts instead of /users:
devise_for :users, :path => 'accounts'
咱們的 :path => '' 說明 /sign_in, /password/new, /confirmation/new 等都升了一級,原來二級連接變爲一級,原來三級連接變爲二級。spa
- :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up, :password, :confirmation, :unlock.
devise_for :users, path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', registration: 'register', edit: 'edit/profile' }
給 sign_in, sign_out 等換名字,這就至關於更換(alise) action 的名字,但內容是不變的。code
- :controllers => the controller which should be used. All routes by default points to Devise controllers. However, if you want them to point to custom controller, you should do:
devise_for :users, :controllers => { :sessions => "users/sessions" }
原來咱們的 controller 是由 devise namespace和sessions, passwords, confirmations controller構成;若是咱們想要從新實現,通常都不會再使用這些構成方式,而是用本身便於理解的來作。orm
擴展一下: 使用 devise 後 /users/* 用到3個controller,它們是:sessions, passwords, registrations繼承
new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_password POST /users/password(.:format) devise/passwords#create new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel user_registration POST /users(.:format) devise/registrations#create new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy (下面兩行是前面已經提到過的,不算) users GET /users(.:format) users#index user GET /users/:id(.:format) users#showget
registrations: "users" 改造後
cancel_user_registration GET /users/cancel(.:format) users#cancel user_registration POST /users(.:format) users#create new_user_registration GET /users/sign_up(.:format) users#new edit_user_registration GET /users/edit(.:format) users#edit PATCH /users(.:format) users#update PUT /users(.:format) users#update DELETE /users(.:format) users#destroy (下面兩行是前面已經提到過的,不算) users GET /users(.:format) users#index user GET /users/:id(.:format) users#show 咱們並非想徹底重寫這裏的全部 action 而是部分。那麼最好的辦法,就是:繼承! 繼承 devise 提供的類,而後重寫咱們想要的 action.... 有哪些 action ? 以咱們實際遇到的項目中,是: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb
在實踐中,registrations: "users"改造後,本來 /users/* 下的請求都到 UsersController 下處理了,若是還 < ApplicationController 那麼 users/sign_up 就不能用。 但咱們 < Devise::RegistrationsController 它又可用了!
因此,驗證了上面說法。1,咱們指定本身的 Controller 來處理;2,繼承 devise;3. 只重寫咱們想定製的部分action