All threads / URL Based multi tenancy

Ask A Question

Notifications

You’re not receiving notifications from this thread.

URL Based multi tenancy

Oyewole Samuel asked in General

I'm new to Ruby on Rails, trying my best to catch up. for example... i've a domain and a username append to it, www.domain.com/samsoft, how can i achieve this and also routing? sample code will be appreciated. Thanks

Hey! Are you looking to do multi-tenancy based upon domain? If so the apartment gem is what you'll want. It can separate the database out by domain and provides some helpers for that. https://github.com/influitive/apartment

I did an episode on the Apartment gem using subdomains but you can modify that to use domains instead of subdomains. https://gorails.com/episodes/multitenancy-with-apartment

The readme for the gem shows all the relevant bits you'll need to change to support domains instead of subdomains.

As for making usernames at the root instead of on /users, you'll need to make a custom route.

resources :users
get ":username", to: "users#show"

The second route will take the /samsoft route and send it to the users show action. You'll need to make sure this route goes at the end of your routes file so that if you ever add something like /help it wouldn't think that "help" was a username.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.