prepend_view_path snippets

Rails prepend_view_path

Tagged rails, prepend_view_path  Languages ruby

Good

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

Bad

# 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 rails, view, prepend_view_path, paths  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'])
end

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.