How to Use Subdomains in a Rails Application
This guide will walk through the process of how to use subdomains in a Rails application, including how to see which subdomain a user is on and how to view subdomains locally.
Guide Tasks
  • Read Tutorial

This will be a fun guide today! Being able to utilize dynamic subdomains in a Ruby on Rails application may seem like a difficult feature to implement, however we'll take it step by step and I think you'll be surprised by how straightforward it is.

In this guide we'll take a high level view of how subdomains work in a Rails application, including:

  • Recognizing what happens when a user is on a subdomain
  • Capturing data from users on subdomains
  • Accessing subdomains while working on a local server

Subdomains on a Local Server

In order to use subdomains on a local server environment we can start up the rails server like normal. From that point there is a special URL to go to in the browser. Instead of localhost:3000 we can use lvh.me:3000.

To see how this works with subdomains, go to somesubdomain.lvh.me:3000 in the browser and you'll see that the application runs normally:

large

Now that we know how to access a subdomain in the browser let's walk through how our app can change its behavior based on what subdomain the user is on.

A Base Case

Whenever I'm building a feature I haven't implemented before it helps me to establish a base case. With that in mind let's do something simple with the subdomain.

Knowing that our ApplicationController controls flow for the entire application, it would be the most logical place for adding some test code. Let's update our ApplicationController file and add in a before_action that logs some data to the console.

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  include Pundit
  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

  protect_from_forgery with: :exception

  before_action :get_subdomain

  def current_user
    super || OpenStruct.new(full_name: 'Guest')
  end

  private

    def user_not_authorized
      flash[:alert] = "You are not authorized to access this page."
      redirect_to(request.referrer || root_path)
    end

    def get_subdomain
      puts "The Subdomain is:"
      puts request.subdomain
      raise
    end
end

In this code the get_subdomain method is called before each page load. In the method right now it's simply printing some data out to the console. Namely it's printing out some debugging text, but it's also printing out the built in method request.subdomain. This is available to us with Rails and let's us know if a user is accessing a subdomain, and if so, what it is.

Also, by placing raise at the end of the method it will stop the application in place to ensure that our debugging output won't get lost with another page load. If you refresh the browser you'll see that this is now working and prints out the subdomain that we're on in the app.

large

What's Next?

In the next lesson we'll extend our ApplicationController method to render a user profile page if someone views a subdomain.

Resources