DEV Community

Mbonu Blessing
Mbonu Blessing

Posted on

How to add and use Subdomains in your rails app

Hello everyone,

This week, I will be showing you how to write subdomains in your routes.rb file.

I was working on an app recently and was thinking of the best way to use the same server to serve different type of users and I discovered how some apps do this.

They use subdomains.

The main domain will be for marketing purposes or for the landing page and other necessary information while subdomains will be created for the different categories of users or products you might want to use.

Let's use google as an example. google.com points you to the search engine.

  • For the mail, you need to use the mail.google.com
  • For docs, slides, sheets or form, you have docs.google.com
  • For drive, you have drive.google.com etc

For this post, we will build a simple route file with 2 subdomain: admin and platform. Admin subdomain will serve admin users while platform subdomain will be used by the apps main users.

To define a subdomain, you use constraints.

# routes.rb

root "home#index"

constraints subdomain: "admin" do
 get "/" => "dashboard#index"
end

constraints subdomain: "platform" do
 get "/" => "platform#index"
end
Enter fullscreen mode Exit fullscreen mode

From the above, we define a root path on the domain and defined two root paths for our different subdomain.

To reference this new path for a redirect or link_to, you just simple need to pass the subdomain to root_url and rails takes it up from there. Using root_path won't work here because it returns a relative path and not an actually path where the subdomain can be included.

# redirects
redirect_to root_url(subdomain: "admin")

redirect_to root_url(subdomain: "platform")

# link_to
link_to 'Dashboard', root_url(subdomain: "admin")

link_to 'Home', root_url(subdomain: "platform")
Enter fullscreen mode Exit fullscreen mode

That's it for this week. Leave questions and comments below.

Until next week.

Top comments (3)

Collapse
 
alexohre profile image
Ohre Alex Oghenebrorhien

hello, thanks for the tutorial but am having some issues with this.
I start ruby on rails about 6 months ago so i don't know much but i discovered one can just type in anything before the localhost and it will work the same way.
eg. hi.localhost:300 is same as localhost:300
and following this tutorial i noticed that when i navigate to admin.localhost:300/dashboard navigatiting to platform from admin url turns platform.admin.localhost:300/platform and am kinda confused.

i mad a navigation link in my application template

<%= link_to 'Dashboard', root_url(subdomain: "admin") %> |
<%= link_to 'Home', root_url(subdomain: "platform") %>

and my routes

root "home#index"

constraints subdomain: "admin" do
get "/" => "dashboard#index"
end

constraints subdomain: "platform" do
get "/" => "platform#index"
end

Note: am using rails 6.1.4
don't know if there a fix to that.

Collapse
 
sabarishcodes profile image
Sabarish Rajamohan

Wow, this is cool stuff. Thanks, Mbonu !!

Collapse
 
nkemjiks profile image
Mbonu Blessing

Thank you for catching that. I have made the update