Method: ActionDispatch::Routing::Mapper#devise_for
- Defined in:
- lib/devise/rails/routes.rb
#devise_for(*resources) ⇒ Object
Includes devise_for method for routes. This method is responsible to generate all needed routes for devise, based on what modules you have defined in your model.
Examples
Let’s say you have an User model configured to use authenticatable, confirmable and recoverable modules. After creating this inside your routes:
devise_for :users
This method is going to look inside your User model and create the needed routes:
# 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"}
Routes integration
devise_for
is meant to play nicely with other routes methods. For example, by calling devise_for
inside a namespace, it automatically nests your devise controllers:
namespace :publisher do
devise_for :account
end
The snippet above will use publisher/sessions controller instead of devise/sessions controller. You can revert this change or configure it directly by passing the :module option described below to devise_for
.
Also note that when you use a namespace it will affect all the helpers and methods for controllers and views. For example, using the above setup you’ll end with following methods: current_publisher_account, authenticate_publisher_account!, publisher_account_signed_in, etc.
The only aspect not affect by the router configuration is the model name. The model name can be explicitly set via the :class_name option.
Options
You can configure your routes with some options:
* class_name: set up a different class to be looked up by devise, if it cannot be
properly found by the route name.
devise_for :users, class_name: 'Account'
* path: allows you to set up path name that will be used, as rails routes does.
The following route configuration would set up your route as /accounts instead of /users:
devise_for :users, path: 'accounts'
* singular: set up the singular name for the given resource. This is used as the helper methods
names in controller ("authenticate_#{singular}!", "#{singular}_signed_in?", "current_#{singular}"
and "#{singular}_session"), as the scope name in routes and as the scope given to warden.
devise_for :admins, singular: :manager
devise_scope :manager do
...
end
class ManagerController < ApplicationController
before_action authenticate_manager!
def show
@manager = current_manager
...
end
end
* 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'
}
* 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" }
* failure_app: a rack app which is invoked whenever there is a failure. Strings representing a given
are also allowed as parameter.
* sign_out_via: the HTTP method(s) accepted for the :sign_out action (default: :delete),
if you wish to restrict this to accept only :post or :delete requests you should do:
devise_for :users, sign_out_via: [:get, :post]
You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
* module: the namespace to find controllers (default: "devise", thus
accessing devise/sessions, devise/registrations, and so on). If you want
to namespace all at once, use module:
devise_for :users, module: "users"
* skip: tell which controller you want to skip routes from being created.
It accepts :all as an option, meaning it will not generate any route at all:
devise_for :users, skip: :sessions
* only: the opposite of :skip, tell which controllers only to generate routes to:
devise_for :users, only: :sessions
* skip_helpers: skip generating Devise url helpers like new_session_path(@user).
This is useful to avoid conflicts with previous routes and is false by default.
It accepts true as option, meaning it will skip all the helpers for the controllers
given in :skip but it also accepts specific helpers to be skipped:
devise_for :users, skip: [:registrations, :confirmations], skip_helpers: true
devise_for :users, skip_helpers: [:registrations, :confirmations]
* format: include "(.:format)" in the generated routes? true by default, set to false to disable:
devise_for :users, format: false
* constraints: works the same as Rails' constraints
* defaults: works the same as Rails' defaults
* router_name: allows application level router name to be overwritten for the current scope
Scoping
Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
scope "/my" do
devise_for :users
end
However, since Devise uses the request path to retrieve the current user, this has one caveat: If you are using a dynamic segment, like so …
scope ":locale" do
devise_for :users
end
you are required to configure default_url_options in your ApplicationController class, so Devise can pick it:
class ApplicationController < ActionController::Base
def self.
{ locale: I18n.locale }
end
end
Adding custom actions to override controllers
You can pass a block to devise_for that will add any routes defined in the block to Devise’s list of known actions. This is important if you add a custom action to a controller that overrides an out of the box Devise controller. For example:
class RegistrationsController < Devise::RegistrationsController
def update
# do something different here
end
def deactivate
# not a standard action
# deactivate code here
end
end
In order to get Devise to recognize the deactivate action, your devise_scope entry should look like this:
devise_scope :owner do
post "deactivate", to: "registrations#deactivate", as: "deactivate_registration"
end
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/devise/rails/routes.rb', line 226 def devise_for(*resources) @devise_finalized = false raise_no_secret_key unless Devise.secret_key = resources. [:as] ||= @scope[:as] if @scope[:as].present? [:module] ||= @scope[:module] if @scope[:module].present? [:path_prefix] ||= @scope[:path] if @scope[:path].present? [:path_names] = (@scope[:path_names] || {}).merge([:path_names] || {}) [:constraints] = (@scope[:constraints] || {}).merge([:constraints] || {}) [:defaults] = (@scope[:defaults] || {}).merge([:defaults] || {}) [:options] = @scope[:options] || {} resources.map!(&:to_sym) resources.each do |resource| mapping = Devise.add_mapping(resource, ) begin raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise) rescue NameError => e raise unless mapping.class_name == resource.to_s.classify warn "[WARNING] You provided devise_for #{resource.inspect} but there is " \ "no model #{mapping.class_name} defined in your application" next rescue NoMethodError => e raise unless e..include?("undefined method `devise'") raise_no_devise_method_error!(mapping.class_name) end if [:controllers] && [:controllers][:omniauth_callbacks] unless mapping.omniauthable? raise ArgumentError, "Mapping omniauth_callbacks on a resource that is not omniauthable\n" \ "Please add `devise :omniauthable` to the `#{mapping.class_name}` model" end end routes = mapping.used_routes devise_scope mapping.name do with_devise_exclusive_scope mapping.fullpath, mapping.name, do routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) } end end end end |