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
Related
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
I have created an application in using Rails 5. My user auth, is managed by the Devise gem.
I need to have different root paths for authenticated and unauthenticated users. I followed the tips given here. Everything seems really straight forward, but after signing in, I am still redirected to the normal root_path when clicking on my 'Home' link for example.
Here is my route.rb code:
authenticated :user do
root to: 'api/v1/private/reporting/dashboards/summaries#index', as: :authenticated_root
end
root to: 'landing#index', as: :root
Here is the code for the 'Home' link in my navbar:
- if api_v1_public_members_user_signed_in?
= link_to 'Home', authenticated_root_path
- else
= link_to 'Home', root_path
Can anybody spot something that I might be missing?
** FYI the 'api_v1_public_members_user_signed_in?' method might look unfamiliar but it's required since I'm namespacing my devise controllers. See here for more information on this.
Try wrapping both your authenticated and unauthenticated root paths under devise_scope and giving them both separate names:
devise_scope :user do
authenticated :user do
root to: 'api/v1/private/reporting/dashboards/summaries#index', as: :authenticated_root
end
unauthenticated :user do
root to: 'landing#index', as: :unauthenticated_root
end
end
Then, change your view to:
- if api_v1_public_members_user_signed_in?
= link_to 'Home', authenticated_root_path
- else
= link_to 'Home', unauthenticated_root_path
The devise docs provide a pattern for this.
Context
Rails 7
Devise 4.8.1
In routes.rb:
unauthenticated do
root "user#index"
end
authenticated :wealth_manager do
root "user#secret", as: :authenticated_root
end
My Rails system was working fine and I was able to access the admin panel without any problems.
But since a couple of days after signup it goes in to the redirect loop, I'm not sure why is this happening. My routes.rb and application_controller.rb are the following.
routes.rb
root :to => 'home#index'
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
match 'ADMIN' => 'admin/admin_users#index'
match 'admin/wallet_withdraws/confirm' => 'admin/wallet_withdraws#confirm'
And application_controller.rb has following code in it.
def authenticate_active_admin_user!
render :text => "Tets" and return
authenticate_admin_user!
unless current_admin_user.role?(:superadmin)
flash[:alert] = "You are not authorized to access this resource!"
redirect_to root_path
end
end
I'm not able to access any link, not just root of admin which is admin/admin_users#index
Any Help will be appreciated.
Thanks!!
Are you by chance using Devise as well? If so, ensure your Devise routes precede your ActiveAdmin routes.
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.
I want to show different root page for users in Rails.
I defined the root:
root :to => 'welcome#index'
And the welcome control:
class WelcomeController < ApplicationController
before_filter :authenticate_user!
def index
end
end
Currently it is ok for logged in users, but the not logged in users redirected to /users/sign_in
I want to show static root page and not redirect.
The answer, suggested by Puneet Goyal will not work in Rails 4. See this. The solution is to use an alias for one of the two routes like this:
authenticated do
root :to => 'welcome#index', as: :authenticated
end
root :to => 'home#static_page'
This answer should work. This was posted on the page Bradley linked.
Put this in your Welcome controller.
def index
if authenticate_user?
redirect_to :controller=>'dashboard', :action => 'index'
else
redirect_to '/public/example_html_file.html'
end
end
In your routes.rb :
authenticated do
root :to => 'welcome#index'
end
root :to => 'home#static_page'
This will ensure that root_url for all authenticated users is welcome#index
For your reference: https://github.com/plataformatec/devise/pull/1147