2012-02-04 70 views
3

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

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 

我试着读了回报率的routes.rb,网址重写,阿帕奇,但无法找出最好的方式来做到这一点。任何关于如何实现这一点的指针?

+0

您是否得到了针对您问题的通用解决方案?我面临同样的问题,并希望避免子域以及 – scanales 2013-03-05 00:06:32

回答

0

您可以通过

http://myapp.com/org1/tasks

最后途径实现这一目标。

在org1/tasks路线之前放置http://myapp.com/loginhttp://myapp.com/tasks的路线。因此,只有在登录和任务路线不匹配,Rails的路由器将寻求更通用的路由

+0

我正在寻找一个更通用的解决方案,这将适用于所有的路线,而不仅仅是/任务..想知道getatisfaction如何实现他们的路由 – Gopi 2012-02-09 09:25:09

2

你可以尝试将范围部分航线:

resources :tasks 

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

在你TasksController,你会得到一个PARAM [ :tenant]变量,您可以使用它来查找租户。如果param [:tenant]为零,则可以返回所有任务。