Devise wrong redirect using devise_scope - ruby-on-rails

If I enter an invalid username or password in
/admin/sign_in.
Devise redirect to /administrators/sign_in instead of /admin/sign_in.
This is my routes.rb
devise_for :administrators
devise_scope :administrator do
get "/admin/sign_in" => "devise/sessions#new"
end
rake routes | grep sing_in:
new_administrator_session GET /administrators/sign_in devise/sessions#new
administrator_session POST /administrators/sign_in devise/sessions#create
admin_sign_in GET /admin/sign_in devise/sessions#new
I'm running:
ruby '2.1.2'
rails '4.2.0'
devise '~> 3.4.1'

That get only adds to your existing routes. You need to stop Devise from creating the default session routes, and then create your own. Try adding changing your routes to:
devise_for :administrators, :skip => [:sessions]
as :user do
get 'admin/sign_in' => 'devise/sessions#new', :as => :new_user_session
post 'admin/sign_in' => 'devise/sessions#create', :as => :user_session
delete 'admin/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
More info in the docs here.

Change your routes to like this
devise_for :administrators, :skip => [:sessions]
devise_scope :administrator do
get "/admin/sign_in" => "devise/sessions#new"
end

Related

Could not find devise mapping for path "/". before the user has signed in

I am getting the following error when i go to root after logging in
Could not find devise mapping for path "/".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
Routes File
Rails.application.routes.draw do
root 'pages#home'
devise_for :users, controllers: { sessions: "users/sessions", registrations: "users/registrations", confirmations: "users/confirmations", passwords: "users/passwords" }, :skip => [:sessions]
as :user do
get 'sign_in' => 'users/sessions#new', :as => :new_user_session
post 'sign_in' => 'users/sessions#create', :as => :user_session
match 'sign_out' => 'users/sessions#destroy', :as => :destroy_user_session,
:via => Devise.mappings[:user].sign_out_via
end
end
Even though i have a route_path it throws up the error.
Try writing routes in scope block.
devise_scope :user do
# write all your routes inside this block
end
You can find more about scope here
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

how to use new_user_session route from devise as the root path?

Problem - I don't know how to set the new_user_session route from devise gem as the root path in Rails app.
Rails.application.routes.draw do
devise_for :users
resources :dashboard
root to: "home#index"
Place this line in routes.rb
devise_scope :user do
root :to => 'devise/sessions#new'
end
You need to set default session route.
Replace with .
devise_for :users, :controllers => {:registrations => "registrations", :sessions => "sessions"}
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
and root to
devise_scope :user do
get "/" => "sessions#new"
end

Devise: Issue with routing using devise_scope

I'm trying to route my User model sign up form to /sign_in. Tried several things and just used the example given in the devise docs.
I copied in to my routes.rb
devise_scope :users do
get "sign_in", to: "devise/sessions#new"
end
and rake routes says it should be working and shows what I expected.
http://s1.postimg.org/do335u8v3/Screen_Shot_2014_06_18_at_11_34_34.png
What's going wrong here?
Try this instead.
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
get "/users/sign_up", :to => "registrations#new"
end

Routing error in devise gem?

I have updates my route.rb like:-
devise_for :authorizes
devise_scope :authorizes do
get '/alogin' => 'devise/sessions#new'
get '/alogout' => 'devise/sessions#destroy'
end
devise_for :hrs
devise_scope :hrs do
get '/hlogin' => 'devise/sessions#new'
get '/hlogout' => 'devise/sessions#destroy'
end
devise_for :employes
devise_scope :employes do
get '/elogin' => 'devise/sessions#new'
get '/elogout' => 'devise/sessions#destroy'
end
ERROR
Could not find devise mapping for path "/alogin". This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]`
When you use devise_scope, you should use singular for your model, i.e. :authorize (not :authorizes)
Just try the following code:
devise_for :authorizes
devise_scope :authorize do
get '/alogin' => 'devise/sessions#new'
get '/alogout' => 'devise/sessions#destroy'
end
devise_for :hrs
devise_scope :hr do
get '/hlogin' => 'devise/sessions#new'
get '/hlogout' => 'devise/sessions#destroy'
end
devise_for :employes
devise_scope :employe do
get '/elogin' => 'devise/sessions#new'
get '/elogout' => 'devise/sessions#destroy'
end

Devise - Changing the name of a routing

By default Devise creates a routing for a sign-in as '/sign_in'.
How would I change the path so that it is '/log_in'?
By adding scope
devise_for :users
devise_scope :user do
get '/login' => 'devise/sessions#new'
get '/logout' => 'devise/sessions#destroy'
end
This is how I did it in the end, given that I was already inserting controllers to handle aspects of Devise:
in routes.rb
#Add Devise authentication to users, handling omniauth callbacks in users/omniauth_callbacks_controller
devise_for :users, :skip => [:sessions],
:controllers => { :omniauth_callbacks => 'users/omniauth_callbacks',
:registrations => 'users/registrations'
}
# :skip => [:sessions] tells devise not to create routes for sessions, allowing us to declare our own
as :user do
get 'users/log_in' => 'devise/sessions#new', :as => :new_user_session
post 'users/log_in' => 'devise/sessions#create', :as => :user_session
delete 'users/log_out' => 'devise/sessions#destroy', :as => :destroy_user_session
end

Resources