class ActionDispatch::Routing::Mapper
def authenticate(scope=nil)
end
resources :users
authenticate(:admin) do
end
resources :post
authenticate do
Allow you to add authentication request from the router:
def authenticate(scope=nil) constraint = lambda do |request| request.env["warden"].authenticate!(:scope => scope) end constraints(constraint) do yield end end
def authenticated(scope=nil)
root :to => 'landing#show'
end
root :to => 'dashboard#show'
authenticated do
end
root :to => 'admin/dashboard#show'
authenticated :admin do
can optionally specify which scope.
Allow you to route based on whether a scope is authenticated. You
def authenticated(scope=nil) constraint = lambda do |request| request.env["warden"].authenticate? :scope => scope end constraints(constraint) do yield end end
def devise_confirmation(mapping, controllers) #:nodoc:
def devise_confirmation(mapping, controllers) #:nodoc: resource :confirmation, :only => [:new, :create, :show], :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations] end
def devise_for(*resources)
end
post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
devise_for :owners, :controllers => { :registrations => "registrations" } do
In order to get Devise to recognize the deactivate action, your devise_for entry should look like this,
end
end
# deactivate code here
# not a standard action
def deactivate
end
# do something different here
def update
class RegistrationsController < Devise::RegistrationsController
For example:
overrides an out of the box Devise controller.
list of known actions. This is important if you add a custom action to a controller that
You can pass a block to devise_for that will add any routes defined in the block to Devise's
==== Adding custom actions to override controllers
end
end
{ :locale => I18n.locale }
def self.default_url_options
class ApplicationController < ActionController::Base
Devise can pick it:
You are required to configure default_url_options in your ApplicationController class level, so
end
devise_for :users
scope ":locale" do
If you are using a dynamic segment, as below:
However, since Devise uses the request path to retrieve the current user, it has one caveats.
end
devise_for :users
scope "/my" do
Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
==== Scoping
* :defaults => works the same as Rails' defaults
* :constraints => works the same as Rails' contraints
devise_for :users, :format => false
* :format => include "(.:format)" in the generated routes? true by default, set to false to disable:
devise_for :users, :skip_helpers => [:registrations, :confirmations]
devise_for :users, :skip => [:registrations, :confirmations], :skip_helpers => true
given in :skip but it also accepts specific helpers to be skipped:
It accepts true as option, meaning it will skip all the helpers for the controllers
This is useful to avoid conflicts with previous routes and is false by default.
* :skip_helpers => skip generating Devise url helpers like new_session_path(@user).
devise_for :users, :only => :sessions
* :only => the opposite of :skip, tell which controllers only to generate routes to:
devise_for :users, :skip => :sessions
* :skip => tell which controller you want to skip routes from being created:
current_publisher_account, authenticate_publisher_account!, publisher_account_signed_in, etc.
and views. For example, using the above setup you'll end with following methods:
Also pay attention that when you use a namespace it will affect all the helpers and methods for controllers
this by providing the :module option to devise_for.
Will use publisher/sessions controller instead of devise/sessions controller. You can revert
end
devise_for :account
namespace :publisher
So the following setup:
Notice that whenever you use namespace in the router DSL, it automatically sets the module.
devise_for :users, :module => "users"
devise/registrations and so on. If you want to namespace all at once, use module:
* :module => the namespace to find controlers. By default, devise will access devise/sessions,
You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
devise_for :users, :sign_out_via => [ :post, :delete ]
if you wish to restrict this to accept only :post or :delete requests you should do:
* :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
are also allowed as parameter.
* :failure_app => a rack app which is invoked whenever there is a failure. Strings representing a given
devise_for :users, :controllers => { :sessions => "users/sessions" }
However, if you want them to point to custom controller, you should do:
* :controllers => the controller which should be used. All routes by default points to Devise controllers.
devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
:password, :confirmation, :unlock.
* :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
devise_for :users, :singular => :user
name in controller, as the name in routes and the scope given to warden.
* :singular => setup the singular name for the given resource. This is used as the instance variable
devise_for :users, :path => 'accounts'
The following route configuration would setup your route as /accounts instead of /users:
* :path => allows you to setup path name that will be used, as rails routes does.
devise_for :users, :class_name => 'Account'
properly found by the route name.
* :class_name => setup a different class to be looked up by devise, if it cannot be
You can configure your routes with some options:
==== Options
POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
# Confirmation routes for Confirmable, if User model has :confirmable configured
POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
# Password routes for Recoverable, if User model has :recoverable configured
destroy_user_session GET /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
# Session routes for Authenticatable (default)
needed routes:
This method is going to look inside your User model and create the
devise_for :users
confirmable and recoverable modules. After creating this inside your routes:
Let's say you have an User model configured to use authenticatable,
==== Examples
defined in your model.
generate all needed routes for devise, based on what modules you have
Includes devise_for method for routes. This method is responsible to
def devise_for(*resources) @devise_finalized = false options = resources.extract_options! options[:as] ||= @scope[:as] if @scope[:as].present? options[:module] ||= @scope[:module] if @scope[:module].present? options[:path_prefix] ||= @scope[:path] if @scope[:path].present? options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {}) options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {}) options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {}) options[:options] = (@scope[:options] || {}).merge({:format => false}) if options[:format] == false resources.map!(&:to_sym) resources.each do |resource| mapping = Devise.add_mapping(resource, options) 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.message.include?("undefined method `devise'") raise_no_devise_method_error!(mapping.class_name) end routes = mapping.used_routes devise_scope mapping.name do yield if block_given? with_devise_exclusive_scope mapping.fullpath, mapping.name, options do routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) } end end end end
def devise_omniauth_callback(mapping, controllers) #:nodoc:
def devise_omniauth_callback(mapping, controllers) #:nodoc: path, @scope[:path] = @scope[:path], nil path_prefix = "/#{mapping.path}/auth".squeeze("/") if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix raise "You can only add :omniauthable behavior to one Devise model" else ::OmniAuth.config.path_prefix = path_prefix end match "#{path_prefix}/:action/callback", :constraints => { :action => Regexp.union(mapping.to.omniauth_providers.map(&:to_s)) }, :to => controllers[:omniauth_callbacks], :as => :omniauth_callback ensure @scope[:path] = path end
def devise_password(mapping, controllers) #:nodoc:
def devise_password(mapping, controllers) #:nodoc: resource :password, :only => [:new, :create, :edit, :update], :path => mapping.path_names[:password], :controller => controllers[:passwords] end
def devise_registration(mapping, controllers) #:nodoc:
def devise_registration(mapping, controllers) #:nodoc: path_names = { :new => mapping.path_names[:sign_up], :cancel => mapping.path_names[:cancel] } resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => mapping.path_names[:registration], :path_names => path_names, :controller => controllers[:registrations] do get :cancel end end
def devise_scope(scope)
devise_for :users
end
match "/some/route" => "some_devise_controller"
devise_scope :user do
good and working example.
noun where other devise route commands expect the plural form. This would be a
Also be aware of that 'devise_scope' and 'as' use the singular form of the
raise ActionNotFound error.
you try to access a devise controller without specifying a scope, it will
Notice you cannot have two scopes mapping to the same URL. And remember, if
end
get "sign_in", :to => "devise/sessions#new"
as :user do
to which controller it is targetted.
you are required to call this method (also aliased as :as) in order to specify
Sets the devise scope to be used in the controller. If you have custom routes,
def devise_scope(scope) constraint = lambda do |request| request.env["devise.mapping"] = Devise.mappings[scope] true end constraints(constraint) do yield end end
def devise_session(mapping, controllers) #:nodoc:
def devise_session(mapping, controllers) #:nodoc: resource :session, :only => [], :controller => controllers[:sessions], :path => "" do get :new, :path => mapping.path_names[:sign_in], :as => "new" post :create, :path => mapping.path_names[:sign_in] match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via end end
def devise_unlock(mapping, controllers) #:nodoc:
def devise_unlock(mapping, controllers) #:nodoc: if mapping.to.unlock_strategy_enabled?(:email) resource :unlock, :only => [:new, :create, :show], :path => mapping.path_names[:unlock], :controller => controllers[:unlocks] end end
def raise_no_devise_method_error!(klass) #:nodoc:
def raise_no_devise_method_error!(klass) #:nodoc: raise "#{klass} does not respond to 'devise' method. This usually means you haven't " \ "loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " \ "inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'" end
def unauthenticated(scope=nil)
root :to => 'dashboard#show'
end
end
root :to => 'devise/registrations#new'
as :user do
unauthenticated do
You can optionally specify which scope.
Allow you to route based on whether a scope is *not* authenticated.
def unauthenticated(scope=nil) constraint = lambda do |request| not request.env["warden"].authenticate? :scope => scope end constraints(constraint) do yield end end
def with_devise_exclusive_scope(new_path, new_as, options) #:nodoc:
def with_devise_exclusive_scope(new_path, new_as, options) #:nodoc: old_as, old_path, old_module, old_constraints, old_defaults, old_options = *@scope.values_at(:as, :path, :module, :constraints, :defaults, :options) @scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults], @scope[:options] = new_as, new_path, nil, *options.values_at(:constraints, :defaults, :options) yield ensure @scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults], @scope[:options] = old_as, old_path, old_module, old_constraints, old_defaults, old_options end