Routing error in devise gem? - ruby-on-rails

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

Related

Redirecting “/” to “/sub-directory” When Running Rails App as a Sub-Directory?

I am running my rails app as sub-directory (for apache mapping reasons):
config.ru:
map '/sub-directory' do
run Rails.application
end
application.rb:
config.root_directory = "/sub-directory/"
config.action_controller.relative_url_root = '/sub-directory'
so now my app (it's root) is ran from the route http://localhost:3000/sub-directory. The problem is when I go to http://localhost:3000/ I get the page:
Not Found: /
I understand I am getting that because my app isn't running in there anymore but I would like that route to redirect to http://localhost:3000/sub-directory. Is there anyway I can accomplish this?
if this helps, routes.rb:
require 'resque/server'
Rails.application.routes.draw do
get "handle/:handle" => "handles#index"
get "/tag/:hashtag" => "tags#index"
get "handles/:handle" => "handles#index"
get "/tags/:hashtag" => "tags#index"
devise_for :users, :controllers => {
:registrations => "registrations",
:passwords => "passwords"
}
devise_scope :user do
get "/email" => "registrations#email"
post "/registrations/check_email" => "registrations#check_email"
get "/users/sign_in" => "registrations#signup"
end
authenticated :user do
devise_scope :user do
root :to => redirect("app"), as: :authenticated_root
get "/app", :to => 'reports#index'
end
end
unauthenticated do
devise_scope :user do
root to: 'registrations#new'
end
end
get '/oauth' => 'social_platforms#new'
get "/auth/:provider/callback" => "social_platforms#create"
get '/login', :to => 'sessions#new', :as => :login
# match '/auth/failure' => 'sessions#failure', :via => [:get]
# get '/reports', to: 'pages#reports', as: :reports
resources :reports, only: [:show, :index]
get '/get_report', to: 'reports#get_report'
get '/check_token', to: 'reports#check_token'
mount Resque::Server.new, at: "/resque"
get '*path' => redirect('/')
end

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

Devise wrong redirect using devise_scope

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

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

Resources