Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6295 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

[PATCH] DRY up rescue_action_locally to make custom rescue templates as easy as they should be.

Reported by: mnaberez Assigned to: David
Priority: normal Milestone: 1.2
Component: ActionPack Version: edge
Severity: normal Keywords: actioncontroller rescue template
Cc: mislav

Description

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

Attachments

remove_hardcoded_path_from_rescue_action_locally.diff (0.7 kB) - added by mnaberez on 09/27/06 06:55:18.
Patch to change rescues_action_locally to use rescues_path instead of a hardcoded relative path.

Change History

09/27/06 06:55:18 changed by mnaberez

  • attachment remove_hardcoded_path_from_rescue_action_locally.diff added.

Patch to change rescues_action_locally to use rescues_path instead of a hardcoded relative path.

09/27/06 10:27:16 changed by mnaberez

The initial report mistakenly lists methods as being mixed in from ActionController::Rescue::ClassMethods. If not obvious, they are instance methods mixed from ActionController::Rescue.

We've since removed our patched rescue_action_locally from ApplicationController and placed a patch for it in lib/ for now, loaded by config/environment.rb.

It would be great to get this tiny patch committed so we no longer need it.

09/29/06 09:04:42 changed by mislav

  • cc set to mislav.

10/09/06 00:40:05 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [5243]) Fixed that rescue template path shouldn't be hardcoded, then it's easier to hook in your own (closes #6295) [mnaberez]

10/10/06 07:46:59 changed by mnaberez

For a short tutorial on how to use custom rescue templates, please see: http://mikenaberezny.com/archives/55.