Problem
Two of our applications use custom rescue templates to provide consistent styling and show our developers a bit more application-specific information during development and staging. We do not need to override ActionController::Base#rescue_action_locally but we do need to specify a different path to the rescue templates.
A hardcoded relative path in rescue_action_locally makes this a pain and causes bloat by making us copy the whole rescue_action_locally method into ApplicationController to change the path even though we don't need it otherwise. This can be fixed with a one line change that doesn't break BC.
Cause
ActionController::Base#rescues_path (mixed in from ActionController::Rescue::ClassMethods) takes a rescue template filename and completes it with an absolute path so it can be rendered.
Switching over to custom rescue templates should be a simple matter of copying the default ones from actionpack/lib/action_controller/templates/rescues into a place like app/views/rescues, modifying to taste, and then overriding rescues_path with three lines in ApplicationController:
def rescues_path(template_name)
"#{template_root}/rescues/#{template_name}.rhtml"
end
This is simple and elegant but it doesn't work. It almost works but the partials for the rescue templates will still be taken from the default path because rescue_action_locally hardcodes the relative path instead of getting it from rescues_path:
@template.instance_variable_set("@rescues_path", File.dirname(__FILE__) + "/templates/rescues/")
This means that even if the default render_action_locally works fine for your application, you still need to copy the entire method into ApplicationController just to fix this path, when there's already a method there just for setting the path (rescues_path).
Solution
Attached is a one-line patch against edge that fixes this by changing rescue_action_locally to use rescues_path. It does this by passing a stub filename to rescues_path and then stripping off the returned filename to get back to the template path.
This change would make using custom rescue templates as easy as it should be when changes to rescue_action_locally aren't needed, DRYs up code in two of our apps, and thus makes us happier.
Please note: This change does not break BC and it is covered by the existing test suite.
Thanks!
- Mike Naberezny