Devise logged in root route rails 3
Think you may have been looking for this:
authenticated :user do
root :to => "dashboard#show"
end
root :to => "devise/sessions#new"
Note: it's authenticate*d*
How to set rails devise sign_in path as a root url
look like you're trying to log in the same user again without a sign out
devise_for :admins, path: 'admins'
devise_scope :admin do
authenticated :admin do
root 'home#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
Rails: Devise after_sign_in_path for multiple resources routes to root
I added:
def after_sign_in_path_for(resource)
stored_location_for(resource) ||
if resource.is_a?(User)
collection_opac_path
elsif resource.is_a?(Admin)
admin_root_path
else
super
end
end
adapting what is specified in devisegem/lib/devise/controllers/helpers.rb
and now the conditional routes work.
How to have root view when user is not logged in rails?
You can use the authenticated
route helper provided by Devise to add a constraint on the routes so that they are only available to logged in users:
Rails.application.routes.draw do
devise_for :users
authenticated :user do
root 'secret#index', as: :authenticated_root
end
root "home#index"
end
The advantage over @Sergio Tulentsev's answer is that this does not cause a redirect or require any additional logic in the controller.
However the reason your app is redirecting to /users/sign_in
is most likely that you are using before_action :authenticate_user!
in your ApplicationController so that the controller handling the root path is requiring authentication and redirecting to the sign in. You can fix this (while retaining a secure by default setup) by skipping the callback:
class HomeController < ApplicationController
skip_before_action :authenticate_user!
# ...
end
This applies to any controller that should not require authentication. You can skip specific actions by using the only
and except
options. Just remember that whitelists are more secure than blacklists.
Rails 4 + Devise: set default root route for authenticated users
Change routes.rb
so that the unauthenticated root
route is wrapped, just like the authenticated one:
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
unauthenticated :user do
root 'pages#home', as: :unauthenticated_root
end
Rails 3: How to set the Devise login like root
If you want to change which action the root_path points to you can do something like in your routes file:
devise_for :users
authenticated :user do
root :to => 'pages#home'
end
root :to => 'pages#welcome'
Setting Devise Login to be root page
To follow on from the people who are asking about the error Could not find devise mapping for path "/"
there is a workaround.
You'll find that there is a clue in your logs which will probably say:
[Devise] 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
match "/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]
So I retried the approach but instead wrapping it (as @miccet suggets) inside a scope block:
devise_scope :user do
root to: "devise/sessions#new"
end
This worked fine for me
Related Topics
Database Configuration Does Not Specify Adapter
Ruby Sleep or Delay Less Than a Second
Where to Put Ruby Helper Methods for Rails Controllers
How to Convert a Ruby Hash So That All of Its Keys Are Symbols
Ruby: How to Convert a String to Boolean
Simple Way of Turning Off Observers During Rake Task
How to Deal with the Conflict Between Activesupport::JSON and the JSON Gem
Differencebetween Integer and Fixnum
How to Decompress Gzip String in Ruby
What's the Difference Between Gem_Home and Gem_Path
Ruby Read CSV File as Utf-8 And/Or Convert Ascii-8Bit Encoding to Utf-8