Change The View Path Of A Controller In Rails

Posted By Weston Ganger

Sometimes you may want to change the view path of a controller. Here are two examples that can be used separately or together.

Shared views for multiple controllers: These controllers will both use the view path app/views/posts:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :set_view_path

  private

  def set_view_path
    case request.host
    when "mysite.com"
      site = "my_site" # root view path will be app/views/my_site
    when "myothersite.com"
      site = "my_other_site" # root view path will be app/views/my_other_site
    else
      site = "default_site" # root view path will be app/views/default_site
    end
    prepend_view_path "#{Rails.root}/app/views/#{site}"
  end
end

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:September 15, 2016