Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 11, 2020 09:17 pm GMT

How to add and use Subdomians in your rails app

Hello everyone,

This week, I will be showing you how to write subdomians 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.rbroot "home#index"constraints subdomain: "admin" do get "/" => "dashboard#index"endconstraints subdomain: "platform" do get "/" => "platform#index"end

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.

# redirectsredirect_to root_url(subdomain: "admin")redirect_to root_url(subdomain: "platform")# link_tolink_to 'Dashboard', root_url(subdomain: "admin")link_to 'Home', root_url(subdomain: "platform")

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

Until next week.


Original Link: https://dev.to/nkemjiks/how-to-add-and-use-subdomians-in-your-rails-app-27n8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To