Method: ActionDispatch::Routing::Mapper#devise_scope

Defined in:
lib/devise/rails/routes.rb

#devise_scope(scope) ⇒ Object Also known as: as

Sets the devise scope to be used in the controller. If you have custom routes, you are required to call this method (also aliased as :as) in order to specify to which controller it is targeted.

as :user do
  get "sign_in", to: "devise/sessions#new"
end

Notice you cannot have two scopes mapping to the same URL. And remember, if you try to access a devise controller without specifying a scope, it will raise ActionNotFound error.

Also be aware of that ‘devise_scope’ and ‘as’ use the singular form of the noun where other devise route commands expect the plural form. This would be a good and working example.

devise_scope :user do
  get "/some/route" => "some_devise_controller"
end
devise_for :users

Notice and be aware of the differences above between :user and :users



363
364
365
366
367
368
369
370
371
372
# File 'lib/devise/rails/routes.rb', line 363

def devise_scope(scope)
  constraint = lambda do |request|
    request.env["devise.mapping"] = Devise.mappings[scope]
    true
  end

  constraints(constraint) do
    yield
  end
end