How to intercept rail template rendering

I have an application that serves more than one website. Like Stack Exchange, these few sites behave very similar to each other.

Given the following view directory structure:

views/ shared/users/index.html.erb app1/users/index.html.erb app2/users/ 

How can I re-write the default template rendering in Rails so that

  • when the App1 UsersController # index is called, it displays app1 / users / index.html.erb
  • when the App2 UsersController # index is called, it understands that there is no index.html.erb template, therefore, to check shared / users / index.html.erb before raising the missing template error

Thank you in advance

+4
source share
2 answers

I know that you have already accepted the answer for this, but I don’t think you need to create your own template resolver.

If I understand your question correctly, you are trying to “tag” your views depending on some aspect of the current state of the application. I did the same as before using this dandy little controller method:

 prepend_view_path "app/views/#{current_app_code}" 

Do this in the before_filter file in your application controller and all your controllers will obey:

 class ApplicationController < ActionController::Base before_filter :prepend_view_paths def prepend_view_paths prepend_view_path "app/views/#{current_app_code}" end end 

Now the rails will first search for “app / views / app1 / users / index.html.erb” when “/ users” is requested if “app1” is the current application.

If he does not find it there, he returns to the default location in "app / views / users / index.html.erb".

Hope this gives you another alternative.

+8
source

I believe that you will have to encode your own template resolver. Perhaps this blog post may help.

+2
source

All Articles