Chanpory
Chanpory

Reputation: 3095

Different view for different subdomains in Rails

I am building a Rails application where I need to do a usability test of three different Views for the same application. My thought is to switch out the default view path depending on the subdomain.

For example, I'd like to be able to define the paths something like:

option1.mysite.com => views/option_1
option2.mysite.com => views/option_2
option3.mysite.com => views/option_3

I'd like to keep the Models and Controllers the same, but switch out the Views depending on the subdomain. What might be the best way to do this?

Upvotes: 1

Views: 423

Answers (1)

pjmorse
pjmorse

Reputation: 9294

We do it something like this:

session[:site] = case request.subdomains.last 
        when "a" then "a"
        when "b" then "b"
        when "c" then "c"
end

That's part of a set_site method in our application controller. Every request checks to see if session[:site] is set; if not, it calls set_site to set it.

In your case, now you just need to introduce logic in your views to present things differently depending on the value of session[:site], but it's even better if your actual view HTML is the same and the major difference is in the CSS. Then you just load different CSS files depending on the value of session[:site].

Upvotes: 1

Related Questions