基于路径的多租户RoR应用程序的URL路由

时间:2012-02-04 10:56:37

标签: ruby-on-rails apache multi-tenant

我正在实施多租户RoR应用程序。使用路径中的第一个段作为租户标识符而不是子域来标识租户。我的理解是getsatisfaction.com实现了这种多租户路由。例如:

http://myapp.com/tenant1/resource代替http://tenant1.myapp.comhttp://tenant2.myapp.com

我希望实现以下路由行为

get the tenant part from myapp.com/segement1/resource
if [segment1] has an entry in our db as a tenant
    then set base_url as [http://myapp.com/segment1], and do the route lookup for /resource
else
    set base_url as [http://myapp.com/] and do the route lookup for /segment1/resource

说明

http://myapp.com/login will not match any tenant, hence will login to the site
http://myapp.com/org1/tasks will match a tenant named org1, get the 'tasks' of org1
http://myapp.com/tasks will not many any tenant, get the task of all orgs

我尝试读取RoR routes.rb,url重写,apache但无法找出最佳方法。有关如何实现这一点的任何指示?

2 个答案:

答案 0 :(得分:2)

您可以尝试确定某些路线的范围:

resources :tasks

scope ':tenant' do
  root to: 'dashboard#index', as: 'dashboard'
  resources :tasks
end

在您的TasksController中,您将获得一个可用于查找租户的param [:tenant]变量。如果param [:tenant]为nil,则可以返回所有任务。

答案 1 :(得分:0)

您可以通过制作

来实现这一目标

http://myapp.com/org1/tasks

最后一条路线。

http://myapp.com/loginhttp://myapp.com/tasks的路由放在org1 / tasks路由之前。因此,只有当登录和任务路由不匹配时,Rails的路由器才会寻找更多通用路由