如何在Rails 3中创建动态根? [英] How to create a dynamic root in Rails 3?
问题描述
我的网络应用中有管理员和普通用户。我想根据他们是谁来使他们的根(/)不同。可以从许多不同的页面访问根目录,因此,如果可以在routes.rb文件中进行此操作,将更加容易。这是我当前的文件。
I have admins and normal users in my webapp. I want to make their root (/) different depending on who they are. The root is accessed from many different pages, so it would be much easier if I could make this happen in the routes.rb file. Here is my current file.
ProjectManager::Application.routes.draw do
root :to => "projects#index"
end
有人可以将我链接到一个示例,该示例可以显示我要进去的方向吗?有什么方法可以将逻辑放入路由文件中?感谢您的所有帮助。
Can someone please link me to an example that can show me the direction to go in? Is there any way to put logic into the routes file? Thanks for all the help.
推荐答案
您只需为根路由创建控制器。
You can just create controller for root route.
class RoutesController < ActionController::Base
before_filter :authenticate_user!
def root
root_p = case current_user.role
when 'admin'
SOME_ADMIN_PATH
when 'manager'
SOME_MANAGER_PATH
else
SOME_DEFAULT_PATH
end
redirect_to root_p
end
end
在您的route.rb中:
In your routes.rb:
root 'routes#root'
PS该示例期望使用 Devise ,但您可以根据需要对其进行自定义。
P.S. example expects using Devise, but you can customize it for your needs.
这篇关于如何在Rails 3中创建动态根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!