routes.rb 里有
<!-- 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里的代码
<!-- lang: ruby -->
devise_for :users
意味着,你可以有以下链接
<!-- 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 了...
相比默认的 devise_for 说说我们用到的参数:
- :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 等都升了一级,原来二级链接变为一级,原来三级链接变为二级。
- :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 的名字,但内容是不变的。
- :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构成;如果我们想要重新实现,一般都不会再使用这些构成方式,而是用自己便于理解的来做。
扩展一下: 使用 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#show
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