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.
Related
Good evening how could I cancel or delete these 2 routes, I also want to know how I can redirect to another site if I am not logged in
enter image description here
You can remove the routes by using skip, then specify the routes you still use. something like this:
devise_for :users, :skip => [:sessions] do
delete "/logout" => "devise/sessions#destroy", :as => :destroy_user_session
post "/admin" => "devise/sessions#create", :as => :user_session
end
Devise already have a feature for auto redirect. Go to application_controller.rb and add this before_action :authenticate_user!
I'm not 100% sure this works now that we've disabled the default session paths. The alternative is to create our own method to override it in application_controller.rb
Something like this:
protected
def authenticate_user!
if user_signed_in?
super
else
redirect_to login_path
end
end
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
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've created a devise user model. There are 2 types of user:
customer
admin
I've accomplished bij creating two 'normal' models: customer and admin. These two models are inheriting from the user model, like so:
class Customer < User
Does anyone know how I can setup a root path per type of user. I want something like this:
authenticated :customer do
root :to => "customer/dashboard#index"
end
authenticated :admin do
root :to => "admin/dashboard#index"
end
UPDATE:
I've solved the problem:
root :to => "pages#home", :constraints => lambda { |request|!request.env['warden'].user}
root :to => 'customer/dashboard#index', :constraints => lambda { |request| request.env['warden'].user.type == 'customer' }
root :to => 'admin/dashboard#index', :constraints => lambda { |request| request.env['warden'].user.type == 'admin' }
although an old question, there is no answer and it could be useful for others.
In rails 3.2 (I have never tested it with anything lower) you can do this in your routes.rb file
authenticated :admin_user do
root :to => "admin_main#index"
end
then have your normal root route further down.
This however, doesn't seem to work in rails 4 as it gives Invalid route name, already in use: 'root' (ArgumentError)(as I have just found out and was searching for a solution when I came across this question), if I figure out a way of doing it in rails 4 I will update my answer
Edit:
Ok so for rails 4 the fix is pretty simple but not so apparent right off the bat. all you need to do is make the second root route a named route by adding an as: like this:
authenticated :admin_user do
root :to => "admin_main#index", as: :admin_root
end
this is documented here but note that it seems like only a temporary fix and so is subject to change again in the future
What you could do is have a single root path, say home#index and in the corresponding controller action perform a redirect depending on their user type.
For instance:
def index
if signed_in?
if current_user.is_a_customer?
#redirect to customer root
elsif current_user.is_a_admin?
#redirect to admin root
end
end
end
Using after_sign_in_path_for should be appropriate. So add this to your application_controller.rb:
def after_sign_in_path_for(resource)
if resource.type == 'customer'
your_desired_customer_path
else
your_desired_admin_path
end
end