我有这样一个嵌套资源routes.rb:
map.resources :users, :only => [:index] do |user|
user.resources :projects
end
Run Code Online (Sandbox Code Playgroud)
它给了我这样的URL /users/2/projects,它将显示所有拥有的项目user2.用户登录后,我希望这是一个根页面,使用map.root.我map.root该如何设置启用此功能?我正在使用设计,所以我可以获得当前用户current_user,但我不确定这是否可用routes.rb.
所以我想做的是根据角色的重定向做重定向current_user.
这就是我所拥有的:
path = case current_user.roles.where(:name => "vendor")
when :vendor
dashboard_path
when :guest
home_path
else
home_path
end
redirect_to path
Run Code Online (Sandbox Code Playgroud)
我正在使用cancan并且唯一的方法来确定用户的角色,我知道,要么做current_user.has_role? :admin或者current_user.roles.where(:name => role_name).
鉴于这些限制(或告诉我另一种方法来确定用户的角色),我如何让这个案例陈述起作用?
编辑1
假设我正在检查多个角色,而不仅仅是我在这里的2个角色 - 可能是4或5.
编辑2
要清楚,这是我目前的设置.
我正在使用Devise,CanCan和Rolify.Rolify允许用户拥有多个角色,但我的应用程序不具备该用例.用户只有一个角色.他们可以是一个vendor, buyer, guest, superadmin.
如果他们是vendor,他们只能看到dashboard_path属于他们的.他们看不到任何其他vendor storefront人属于他人.他们也不应该看到其他供应商的产品.所以,一旦他们登录,他们root_path应该dashboard_path不是home_path每个其他角色root_path将是什么.
如果他们是guest,他们可以看到除价格以外的所有东西 - 我已经有了这个逻辑.我这样做了:
if user.has_role? :guest
can :read, [Product, Vendor, Banner]
cannot :view_prices, Product
end
Run Code Online (Sandbox Code Playgroud)
然后在我看来,我只是做了这样的事情:
<% if …Run Code Online (Sandbox Code Playgroud)