【问题标题】:URL routing for multitenant RoR application based on path基于路径的多租户 RoR 应用程序的 URL 路由
【发布时间】:2012-02-26 18:16:02
【问题描述】:

我正在实施一个多租户 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 rewrite、apache 但无法找出最好的方法。关于如何实现这一点的任何指示?

【问题讨论】:

  • 您是否找到了解决问题的通用方法?我也面临同样的问题,也想避免使用子域

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


【解决方案1】:

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

resources :tasks

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

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

【讨论】:

    【解决方案2】:

    您可以通过制作来完成此操作

    http://myapp.com/org1/tasks

    最后一条路线。

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

    【讨论】:

    • 我正在寻找一个更通用的解决方案,它适用于所有路由,而不仅仅是 /tasks.. 想知道 getatisfaction 如何实现他们的路由
    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2021-08-08
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多