Rails: Adding view paths on the fly
Rails offers a way to prepend (or append) view paths just for the current request.
A use case of this is a different set of view templates that should be used under certain circumstances:
class UsersController < ApplicationController before_action :prepare_views def index ... end private def prepare_views if <condition> prepend_view_path Rails.root.join('app', 'views', 'special') end end end
<condition> is true, Rails will first look into
app/views/special to find a view template. E.g. in this example, it would look for
app/views/special/users/index.erb before trying the default
If the desired template does not exist there, it will fall back to its standard view paths. This way you can customize select templates without needing to redefine all.