views:

1207

answers:

3

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.

A: 
+1  A: 

Solution 2:

Use 'rescue_from' in ApplicationController



class ApplicationController > ActionController::Base
  rescue_from ActionView::MissingTemplate do |exception|
    # use exception.path to extract the path information
    # This does not work for partials
  end
end



Drawback: does not work for partials.

neat!, i will use this too, thanks
Fer
A: 

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.


module ActionView
  class PathSet

    def find_template_with_exception_handling(original_template_path, format = nil, html_fallback = true)
      begin
        find_template_without_exception_handling(original_template_path, format, html_fallback)
      rescue ActionView::MissingTemplate => e
        # Do something with original_template_path, format, html_fallback
        raise e
      end
    end
    alias_method_chain :find_template, :exception_handling

  end
end
Jeroen van Dijk