Multi Subdomain Rails 5 App

I am involved building a SaaS application for Brevica, a US based firm, an app called Church Foyer. Last week I was implementing subdomain based multi-tenancy. For example, you can create a tenant with your own personalized subdomain. That is a cool feature, but I will talk about that later. I am going to pull an example from my personal website.

Here I am going to show you how you can create a single app that has multiple sub-domains. For example:

  • : the admin dashboard
  • : the blog etc

I recommend breaking down a large app to smaller, more manageable apps. But if that is not your interest, this is how you are going to do it. Here I will present you…

  • Session management
  • Rails 5 routing constraints
  • A basic Nginx config for subdomain based Rails app

Step 1: Subdomain Session management

We don’t want our users to login to every single subdomain our app has, agreed? Then we need to configure Rails session store allow this. You can do some thing going in line with…

Rails.application.config.session_store :cookie_store,
                                       key: '_jdeencom_session',
                                       domain: {
                                         production: '',
                                         development: ''
                                       }.fetch(Rails.env.to_sym, :all)

This will allow sessions created in, lets say, be available in,, should the user need to comment on a blog post.

Step 2: Routes and Routing Constraints

Next you will need to define routes that have constraints constraints. This constraints should be in the subdomain level. For example we will need to redirect the request to / when the request is for example to blog#index action.

Rails.application.routes.draw do
  # ...
  constraints JDeen::Constraints::Blog do
    root 'blog#index'
  # ...
# JDeen website main namespace
module JDeen 
  # Routing constraints
  module Constraints
    # Constraints for the blog subdomain
    class Blog
      # Checks if a request to the Rails app matches the blog
      def self.matches?(request)
        request.subdomain.present? && request.subdomain == 'blog'

Step 3: Nginx config

The next is, we would need minor changes to a general Nginx config file. Just notice the * in front of the server name below.

cat /etc/nginx/sites-enabled/
server {
  listen 80;
  return 301 $scheme://$request_uri;

upstream jdeendev {
    server localhost:3500 max_fails=3 fail_timeout=30;

server {
    listen 80;
    server_name *;

    root /home/ziyan/Work/JDeen/JDeen;

    try_files $uri/index.html $uri @jdeendev;

    location @jdeendev {
        proxy_pass http://jdeendev;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;

Thats it and you should be good to go! Unless I missed some thing :relaxed:

A Happy New Year To You

This is one of the most important times of the year for a Sri Lankan. It is the Sinhalese and Hindu new year, the local new year. It is an important time in the year for me and my family, we are Malay Muslims though. I make this an opportunity to wish you a happy new year, regardless of your ethnicity and your religion.

The generally accepted new year is just a change in calendar numbers. People get excited for no apparent reason. The Sinhalese and Hindu new year is different. It reassembles the transition of the sun from Pisces to Aries. You can read a lot more abut the festival here.

I am looking forward to prepare milk rice and enjoy some Kavum, Kokis and other tasty sweets.

While I wish you a wonderful new year, I hope you will be able to accomplish your dreams in the year to come.

Coke vs Pepsi - Halloween

Some would say peace is good, but some times war is interesting. I don’t mean armed conflict but the competition between humans.

Here is some thing interesting post I found from the HubSpot blog.

Pepsi vs Coke

I guess this is when we have to say Coke - 1 : Pepsi - 0.

Enjoy your Thursday, time to dive in my new Ruby project.

VS Code - Ubuntu Icon

Since about a month, I have been trying Visual Studio Code. It is great and developed in a fast pace. I prefer VS Code over Atom editor because it supports Ruby debugging through a plugin. How ever one problem I noticed was that that once installed the VS Code Icon will not show on Unity and instead a default text editor icon was used.

The issue is with the Unity launcher config. You can easily update it. I am on Ubuntu 16.04 LTS so if you are on another distribution I am not sure if this will work.

Make sure you have the icon installed

-> ls /usr/share/code/resources/app/resources/linux/

Then update the launcher config.

sudo vim /usr/share/applications/code.desktop

Update the Icon= line from code to /usr/share/code/resources/app/resources/linux/code.png.

Thats it and that beautiful icon will show nice and big in Unity when you search for code or open it.

The Root Cannel Filling

A trip to a dental can be scary, but it doesn’t have to be so. Among many incapable, there are some who really excel in their jobs, just as in any field. One place I can trust to be good is the Primrose Dental. Thats where mum wanted to get her root-cannel-filling done for one of her tooth.

While we asked for Dr. Prasad Amarathunga, as for my understanding the proprietor of the facility, a former dean of the Faculty of Dental Science - University of Peradeniya, we were given Dr. Wimalarathne. He is a good dentist.

We were told that the root cannel filling would take 5 day. Additionally if we wanted to put up a crown we will need to have 2 more days. Since we are from Colombo, he said he will try to finish the root cannel filling in 3 days.

In practice, it took a little longer. We had to come 4 days to complete the root cannel filling.

The root cannel filling involves drilling through the roots to destroy it. This is done under anesthesia so it doesn’t hurt. Then metal shafts are added and x-rays used to determine if the whole root has been cleared of the nerve. Then it is filled up and finally a permanent filling placed.

Since the root has been destroyed, the tooth will loose strength. This makes it fragile and may break off in the future. To avoid this, we can use a crown. The crown will cover the tooth protecting it from what you bite.

To facilitate the crow, the tooth is cleared all around reducing it to a stem. Then a temporary crown will be placed to protect the tooth until the permanent crown is prepared.

We will be going in one weeks time and hopefully it will be the last.

It is a pain to keep the mouth open for so long for so many days. But a tooth is important.

The whole process would have cost us about Rs. 35,000 ~ 40,000.