简体   繁体   English

带有布尔值的布尔值在true中的不同路线

[英]different routes when boolean is true in rails with devise

This is my routes.rb 这是我的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 . 我的用户表有一列:admin => true/false How could I check and root :to a different controller, when the flag is true. 当标志为true时,如何检查并root :to其他控制器。

Only thing i know is that I can handle it in the views with if current_user.admin? 我唯一知道的是我可以使用if current_user.admin?在视图中处理它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 在您的application_controller.rb中编写类似

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. 或您为admin / user拥有的任何名称,只要它是用户的布尔值即可。

then in your pages_controller.rb then scope out as needed, if you want to deny all access just do a 然后在您的pages_controller.rb中,然后根据需要扩大范围,如果您想拒绝所有访问权限,只需执行

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. 在确定用户是管理员之后,您可以简单地将用户重定向到home#index操作内部的管理页面。 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. 我认为根本不适合在routes文件中有条件地更改主页。

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. 另外,我相信在应用程序启动后,仅会加载一次routes.rb文件,因此我不确定任何条件逻辑都不会有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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