简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM