prepend_view_path snippets

Rails prepend_view_path

Tagged prepend_view_path, rails  Languages ruby


class ApplicationController < ActionController::Base
  prepend_view_path 'x/y/z/views'


# config/initializer/view_path.rb
ApplicationController.prepend_view_path 'x/y/z/views'

In development mode (Rails 3.x) this doesn't seem to work under some circumstances, i.e. the prepended path is lost after Rails' reloads after you modify certain files.

How to prepend a directory to the view path in Rails (3, 4, etc)

Tagged prepend_view_path, view, paths, rails  Languages ruby

prepend_view_path is your friend when you want to use different views in the same app for different configurations, e.g. same code for different apps/sites/etc:

class ApplicationController ....
   prepend_view_path Rails.root.join('app', 'views', ENV['APP'])

prepend_view_path can also be called e.g. once per request before rendering the view to have different view templates for each subdomain, user, cat, etc.