设计基于角色的路由

设计基于角色的路由

本文介绍了设计基于角色的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个用户的应用程序。每个用户作为理论角色(用户,客户端等)。我设计为每个用户类型视图/控制器。

I have an app with multiple users. Each user as a theoretical role (user, client, etc). I've designed a view/controller for each user type.

我希望能够登录每种类型的用户做不同的根URL,并将其锁定它。

I want to be able to login each type of user do a different root url and lock them to it.

本来我是打算到列在制定所谓的角色添加到用户和这样我就可以区分用户。我遇到的问题是如何在routes.rb中说,如果current_user.role ==客户根:为=>'控制器#索引

Originally I was going to add a column to Users in Devise called role and so I can differentiate the users. The problem I'm having is how to say in routes.rb if current_user.role == "client" root :to => 'controller#index'

一旦在页面我也想阻止他们能够访问我的任何其他路径记录,即:domain.com/calls domain.com/units

Once they are logged in to the page I also want to keep them from being able to visit any of my other paths ie: domain.com/calls domain.com/units

我一直在寻找到康康舞并肩设计运行,但我不知道如果这就是答案。

I've been looking into cancan to run alongside Devise but I'm not sure if this is the answer.

推荐答案

而不是路由处理它,为什么不能在ApplicationController中处理呢?

Instead of handling it in routes, why not handle it in ApplicationController?

#application_controller.rb
before_filter :direct_to

def direct_to
  if current_user.role == "client"
    redirect_to client_controller_path
  # etc
end

这篇关于设计基于角色的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:47