ruby-on-rails - 基于路径的 Multi-Tenancy RoR 应用程序 URL 路由

标签 ruby-on-rails apache multi-tenant

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

http://myapp.com/tenant1/resource而不是 http://tenant1.myapp.com , http://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,但无法找到执行此操作的最佳方法。关于如何实现这个的任何指示?

最佳答案

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

resources :tasks

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

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

关于ruby-on-rails - 基于路径的 Multi-Tenancy RoR 应用程序 URL 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9140344/

相关文章:

ruby-on-rails - 使用 Google Calendar API 在 Rails 应用程序中处理时区

regex - 重定向所有子目录但不重定向父目录

java - 带有 Eclipse 链接的 Multi-Tenancy

ruby-on-rails - 可以让 Rails 4 在 Windows 上工作吗?

ruby-on-rails - Ruby on Rails 和 MS SQL EXPRESS

java - 如何使用 apache avro SchemaBuilder 更新现有的 avro 模式?

svn - 如何在命名虚拟主机的根目录中启用mod_dav_svn?

python - 值错误 : No module named 'notmigrations' during unit tests

python - 将一个应用程序用于多个 uwsgi 实例

ruby-on-rails - 三元运算符和html