Rendering different views depending on the hostname of your app

Quite an interesting problem. How to render different views depending on the hostname of your application. Well actually only certain views have to be overridden. The ideal behaviour would to be to set a search path based on the hostname of the application which then falls back to the default if no views exists.

Initially I looked at how I could force searching different view path and using that file if it already exists else use the default by overriding existing ActionView methods.

http://rpheath.com/posts/122-needing-to-extend-rails-view-path

However, this isn’t necessary as it appears Rails supports a method of adding another search path for views by pushing the custom view dir onto the view_paths array. This could be placed in config/initializers/action_controller_extender.rb and loaded as the application is started.

ActionController::Base.view_paths.unshift(File.join(RAILS_ROOT, 'app', 'views', 'custom_views'))

In order to fix this based on the hostname, its actually more convenient to move this into a before filter and on every request we can check the host header.

class ApplicationController < ActionController::Base

  before_filter :custom_view_path

  private

  def custom_view_path
    if ['www.zombification.net'].include?(request.host)
      self.prepend_view_path(File.join(RAILS_ROOT, 'app', 'views', 'zombification'))
    end
  end

end