3
votes

I am implementing a multitenant RoR application. Tenants are identified using the first segment in the path as the tenant identifier instead of subdomains. My understanding is that getsatisfaction.com implements this kind of multitenant routng. Ex:

http://myapp.com/tenant1/resource instead of http://tenant1.myapp.com, http://tenant2.myapp.com

I am looking to implement the following routing behaviour

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

To illustrate

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

I tried reading up RoR routes.rb, url rewrite, apache but unable to figure out the best way to do this. Any pointers on how to implement this?

2
did you get a generic solution to your problem? I'm facing the same issue and want to avoid subdomains as well - scanales

2 Answers

2
votes

You could try scoping some routes:

resources :tasks

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

In your TasksController, you'll get a param[:tenant] variable that you can use to look up the tenant. If param[:tenant] is nil, you can just return all tasks.

0
votes

You can accomplish this by making

http://myapp.com/org1/tasks

the last route.

Put the routes for http://myapp.com/login and http://myapp.com/tasks before the org1/tasks route. So only if the login and tasks route are not matched, Rails' router will look for more generic routes