Search code examples
ruby-on-railssubdomain

Serve dashboard in subdomain in Rails


I am building an app using multitenancy and apartment gem After verification of email, a user is redirected to their subdomain

What I want to do is serve the post login dashboard for the admins on a single subdomain, lets say app.domain.com instead of the subdomain they request at the time of signup.

So their dashboard will be located at app.domain.com and their main app will be located at the subdomain they chose at the time of signup.

Is there anyone who can point me in the right direction here?

This is my current approach in the routes

require 'subdomain'
  constraints(Domain) do
    root :to => "pages#home"
  end

  constraints(Subdomain) do
    root :to => "pages#store"
  end

lib/subdomain.rb

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end
end

class Domain
  def self.matches?(request)
    !request.subdomain.present? or request.subdomain == "www"
  end
end

Solution

  • One way to do that is to separate the business logic into public facing and admin facing properties as you are doing. If you do that then you can achieve what you want easily. Another way is to create a multi engine rails application where you could keep admin and public things in separate engine. In my humble opinion both ways are acceptable. If you want to know more about multi-engine application then you could read this great example

    https://github.com/taskrabbit/rails_engines_example