Devise not redirecting where I would expect - ruby-on-rails

Just migrated from Authlogic to Devise, and having a redirect issue.
I have the following:
root :to => "welcome#index"
authenticated :user do
root :to => "dashboard#show"
end
However, after loggin in, I end up on welcome#index, and not on dashboard#show as I would expect.
The devise documentation says:
After signing in a user, confirming the account or updating the
password, Devise will look for a scoped root path to redirect.
Example: For a :user resource, it will use user_root_path if it
exists, otherwise default root_path will be used.
Which only reinforces my expectation.

def after_sign_in_path_for(resource_or_scope)
new_order_path
end
Define this in your applications controller. This will route your user to a particular path after sign_in.
Additional tidbit:
If you want to route the user to a particular page after confirming through email use this in your applications controller.
def after_confirmation_path_for(resource_or_scope)
end
Try this:
resources :dashboard
authenticated :user do
root :to => "dashboard#show"
end
make sure the
root :to => "path"
after the above code and not below that.

Related

Rails: Devise after_sign_in_path for multiple resources routes to root

I am working on a library catalogue app, which authenticates admins and users. Somehow I am unable to set the after_sign_in_path_for, since login routes now to the public root, instead of the root of each namespace.
I had already implemented an admin namespace and admin authentication and everything was working. In order to customize the sign_out path I had added:
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
if resource_or_scope == :user
collection_path
elsif resource_or_scope == :admin
new_admin_session_path
else
root_path
end
end
I had already included the user scope in this section, even if I had not implement the resource yet. Everything was working.
Now I want to add a user resource and collection namespace where the catalogue is placed. Since I now need to specify the upon sign_in redirect, I completed the application_controller.rb with:
# Overwriting the sign_in redirect path method
def after_sign_in_path_for(resource_or_scope)
if resource_or_scope == :user
collection_opac_path
elsif resource_or_scope == :admin
admin_root_path
else
root_path
end
end
Somehow I am now redirected to the main root instead of collection_opac_path or admin_root_path. The later was working before defining the after_sign_in_path.
In my route.rb I have the following entries:
devise_for :users
devise_for :admins
namespace :collection do
match '/', :to => 'opac#home'
match '/opac', :to => 'opac#opac', :as => :opac
root :to => 'opac#home'
end
namespace :admin do
...
root :to => 'pages#pageadmin'
end
root :to => 'pages#manifesto'
Admin is authenticated on all controllers of the admin namespace. User is authenticated on the opac action of the opac controller in the collection namespace.
rake routes gives:
collection /collection(.:format) collection/opac#home
collection_opac /collection/opac(.:format) collection/opac#opac
collection_root /collection(.:format) collection/opac#home
admin_root /admin(.:format) admin/pages#pageadmin
root / pages#manifesto
What am I doing wrong? How can I get this to work?
Thanks in advance!
Update
I think it is important to specify that the user login form is placed in the collection/home view and upon submit & login should redirect to collection/opac, while the admin login form appears when trying to access the root of the admin namespace.
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 do you customize the route after a user logs-in or signs-in in Devise?

Currently, a user gets redirected to a root index page after sign-in/log-in but I want to customize it so that the redirect goes to a different page.
My route file is:
Rails.application.routes.draw do
devise_for :admins, path: 'admins'
root 'home#index'
get '/' => "courses#index", as: :user_root
devise_for :users, path: 'users'
resources :courses, :lessons
end
I understand that by default, the root become where the redirect goes to so I used the code get '/' => "courses#index", as: :user_rootand the redirect worked as I wanted to. However, when a user logs-out, the redirect tries to go to get '/' => "courses#index", as: :user_rootonce again which I do no want. Instead, I want the redirect when a user logs-out to go to root 'home#index'.
So essentially my question is how can I customize my root so I can achieve different redirects depending on whether a user logs-in/signs-in and logs-out?
You can use this:
class ApplicationController < ActionController::Base
private
def after_sign_in_path_for(resource)
user_root_path
end
def after_sign_out_path_for(resource_or_scope)
root_path
end
end
It is on devise wiki:
signing in
signing out

different routes when boolean is true in rails with devise

This is my routes.rb
Devel::Application.routes.draw do
authenticated :user do
root :to => 'home#index', as: :authenticated_user
end
root :to => 'welcome#index'
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users
end
My user table has a column :admin => true/false. How could I check and root :to a different controller, when the flag is true.
Only thing i know is that I can handle it in the views with if current_user.admin?.
Any suggestion or ideas?
best regards
denym
I have a similar setup, wrote my own with warden...
in your application_controller.rb
write something like
def authorize_admin
redirect_to root_url, alert: 'Access Denied' if current_user.admin == false
end
or whatever name you have for admin/user, just as long as its a boolean on your user.
then in your pages_controller.rb
then scope out as needed, if you want to deny all access just do a
before_action :authorize_admin
otherwise nest it under any def you like...
def action
authorize_admin
blah blah blah
end
You could simply redirect the user to the admin page inside of the home#index action, after you've determined that they are an admin. Make sure that said page is access restricted, however, just in case someone goes there manually, or gets redirected on accident.
I don't think it's appropriate to change the home page conditionally in the routes file at all.
Also, I believe that the routes.rb file is loaded just once, upon the application starting, so I'm not sure that any conditional logic would be effective anyway.

Rails Devise roots, what's the difference between namespace and authenticated?

In a Rails 3.2 app I have two user models set up using Devise: Admin and User.
I have an Admin-specific root page defined in my routes file.
namespace :admin do
root :to => "pages#welcome"
end
I have also seen this written as
authenticated :user do
root :to => 'pages#welcome'
end
I have not been able to find a clear description of the difference between using namespace and authenticated, and what implications this may have for security.
I'd be very grateful if someone could enlighten me, or point me towards a clear description.
Thanks!
Namespace routes will always exist -- meaning you can always call
/admin and it will route to pages#welcome
Authenticated routes will only exist if the :user is logged in.
I use it to create a root_path that's a dashboard for a logged in user, but the marketing pages#welcome for non logged in:
authenticated :user do
root :to => 'profile#show'
end
root :to => 'pages#welcome'
Here is a link to devise doc for the method authenticated, which should answer your question : http://rdoc.info/github/plataformatec/devise/ActionDispatch/Routing/Mapper#authenticated-instance_method

How to set the signup page as homepage

I've just installed Devise in my RoR app.
I want my root site ( http://site.com ) displays the devise sign up page.
If the user is already logged, then redirect to the user dashboard. But, if an user go to http://site.com/dashboard and is not logged, then redirect to the home page, where the user can see the sign up.
How can I do this?
Thank you
UPDATE:
In my routes.rb there is
root :to => 'users#index'
and this in my users_controller:
def index
if user_signed_in?
render 'dashboard'
else
redirect_to new_user_registration_path
end
end
It's correct?
Add the following to your routes.rb
authenticate :user do
root :to => "user_dashboard#show"
end
root :to => "devise/sessions#new"
Change the "user_dashboard#show" to your controller#method for your dashboard.
authenticate is a devise specific method for your routes file.
Source:
http://rdoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper#authenticate-instance_method

Resources