For a plugin I want to hack the following feature into Rails:
When a (partial) template does not exist (regardless of the format) I want to render a default template.
So say I call an action 'users/index' if users/index.html.erb does not (or other format) exist, 'default/index.html.erb' should be rendered.
Similarly, If I call an action 'locations/edit' and 'locations/edit.html.erb' does not exist, 'default/edit.html.erb' should be rendered
For partials, If I call an action 'locations/index' and the template 'locations/index.html.erb' calls the partial 'locations/_location' which does not exist, it should render 'default/_object'
The solution is seek gives me access to the templates variables (e.g. @users, @locations) and information on the requested path (e.g. users/index, locations/edit). And it should also work with partials.
I have thought of some options which I'll post below. None of them are completely satisfactory.
I found a patch that is relatively clean, it only patches the lookup of the template which is exactly what was required in the question.
Solution 1:
Monkey patch ActionView::Base#render
This monkey patch requires to look into the (changing) internals of rails and results in ugly code, but probably works.
Rails 3.1 automatically looks for files in application/template.html.erb after looking in controller/template.html.erb you can see this in the Exception like so:
so, just put your default templates in app/views/application
Solution 2:
Use 'rescue_from' in ApplicationController
Drawback: does not work for partials.