Posted 7 months ago. Visible to the public. Repeats.

Rails: Adding view paths on the fly

Rails offers a way to prepend (or append) view paths just for the current request.

Example

A use case of this is a different set of view templates that should be used under certain circumstances:

Copy
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

If <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 app/views/users/index.erb.

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.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Dominik Schöler
Last edit:
6 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more