Devise_scope - Could not find devise mapping for path "/" - ruby-on-rails

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]
I am getting this error when I try to go here
http://localhost:3000/
And here are the routes
devise_scope :user do
get '/users/sign_in' => 'devise/sessions#new', as: :new_user_session
post '/users/sign_in' => 'devise/sessions#create', as: :user_session
delete '/users/sign_out' => 'devise/sessions#destroy', as: :destroy_user_session
post '/users/password' => 'devise/passwords#create', as: :user_password
put '/users/password' => 'devise/passwords#update', as: nil
patch '/users/password' => 'devise/passwords#update', as: nil
authenticated :user do
root :to => 'home#index', as: :authenticated_root
end
unauthenticated :user do
root :to => 'devise/sessions#new', as: :unauthenticated_root
end
end
This set up was working earlier so I don't know why it is suddenly failing. Details about my setup (from memory): I did
bundle install
with devise as a gem
rails g devise user
rails generate devise:views
I added this line in development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Also, I am using rails 4.2.0.beta1 and devise 3.3.0 pulling from (git => 'https://github.com/plataformatec/devise.git', :branch => 'lm-rails-4-2')

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

Related

Rails - devise and sidekiq routes

I am running a Rails 5.0.0 app with Ruby 2.3.1
Sidekiq is being used for background jobs and devise for authentication.
Sidekiq monitoring and devise are mounted in routes as follows:
devise_for :users, skip: [:sessions]
as :user do
get 'login' => 'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
require 'sidekiq/web'
require 'sidekiq/cron/web'
#Sidekiq::Web.set :session_secret, Rails.application.secrets[:secret_key_base]
authenticate :user do
mount Sidekiq::Web => '/sidekiq'
end
But, accessing the sidekiq status page logs out the user.
The same code used to work fine with Rails 4.2.5
Try wrapping your mounting of Sidekiq under devise_scope, in the same way you're using its alias "as" in your devise_for route:
# Only allow authenticated users to get access
# to the Sidekiq web interface
devise_scope :user do
authenticated :user do
mount Sidekiq::Web => '/sidekiq'
end
end
Here's a snippet for that allows for custom authentication on the Sidekiq routes.
authenticate :user, ->(user) { user.admin? || Other auth related checks... } do
mount Sidekiq::Web => "/sidekiq"
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

undefined method `session_path'

I am using Rails + Devise + OmniAuth + Google OAuth2.
My user model (user.rb) contains:
devise :registerable, :omniauthable, :omniauth_providers => [:google_oauth2]
My routes.rb look like:
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
post 'sign_in', :to => 'devise/session#create', :as => :user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
get 'services', to: 'static_pages#services'
get 'my_account', to: 'my_account#index'
get 'invite', to: 'invite#show'
get 'invite/:id', to: 'invite#show'
root 'static_pages#home'
end
When I go to /sign_in, I get an exception like:
undefined method `session_path' for #<#<Class:0x007f9b7173af28>:0x007f9b713d8da8>
in:
~/.rvm/gems/ruby-2.1.1/gems/devise-3.2.4/app/views/devise/sessions/new.html.erb
in line:
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
If I add :database_authenticatable to user.rb it all starts working, but I want my users to be able to sign-in through Google OAuth2 only, so I don't want :database_authenticable. It looks like session_path is not available for some reason, but I am not sure why and how to make it available.
Thanks,
Jen
You need to reboot the rails server. That was the solution for me.
I do believe that, as you use devise_scope for the sessions paths, you need to add skip to your devise_for call, like so:
devise_for :users, skip: [:sessions], controllers: { omniauth_callbacks: 'omniauth_callbacks' }
Doing so will not generate the route helpers for the sessions controller
If you add or modify anything in the devise configuration, you need to reboot the rails server. just stop and run the rails server command again

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