2

I have searched around the web and there are answers that have helped me abit, however I am still stuck, so here goes.

I a Rails 4 app that allows users to create a biography/blog and then access it using their own domain.

Users can choose from several pre-made website templates (main page, about me page, my hobbies page, etc...), and then they load up their content using a CMS. The content will then be displayed using their chosen template when visitors visit their domain.

Eg:

User 1:

Domain: www.user1.com

Template: Template A

User 2:

Domain: www.user2.com

Template: Template B

Desired Results

When a visitor visits www.user1.com, they will see the main page. When they click on "About Me", they will be redirect to www.user1.com/about-me. If a visitor visits the "About Me" page for user 2, they will see www.user2.com/about-me.

My question here is, how do I set this up?

Based on this answer: Rails routing to handle multiple domains on single application

class Domain
  def self.matches?(request)
    request.domain.present? && request.domain != "mydomain.com"
  end
end

------in routes.rb------

require 'subdomain'
constraints(Domain) do
  match '/' => 'blogs#show'
end

I know I can route a different domain compared to mine to a separate controller, however, I need to route it to different template controllers which can change at any moment (users can change templates at will).

I know I can set up a general controller that can read incoming requests, then based on the hostname, I can extract the appropriate template and then redirect the request to that template's controller (eg: Template1Controller), however the url gets messed up, becoming something like "/template/template1/index" or "/template/template1/about-me" which is very bad and ugly. Furthermore, it will be extremely tricky to handle paths specific to only some templates (Template A might have a "My Resume" page while template B might have a "Family History" page instead).

Is there a way to do this?

I have thought about a method where I have a single controller that will handle everything (without redirects) and then just calls render template1/index, but I think it is a bad way of doing it (different template might need different data in each page).

Btw, this will be hosted on EC2.

EDIT What I am looking to implement is quite similar to this question Mapping multiple domain names to different resources in a Rails app , but unfortunately no answers then. Im hoping 5 years later, someone might know how to get this done.

Thanks!

Community
  • 1
  • 1
Tikiboy
  • 892
  • 10
  • 20

1 Answers1

0

I do this pretty simple with Heroku. It's probably not hard anywhere.

Once you have DNS set up.. the Rails layer can look like...

  1. Create a before_filter in ApplicationController. before_filter :domain_check

  2. In my domain_check method I just have if request.host ~= /whatever/ do this elsif ... elsif ... end

"do this" can be a redirect or a render or whatever.

slindsey3000
  • 4,053
  • 5
  • 36
  • 56