Created
May 5, 2009 17:02
-
-
Save heisters/107057 to your computer and use it in GitHub Desktop.
automatically define Rails render_* methods for all possible HTTP responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protected | |
# Define render_xxx methods for all known status codes. | |
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |symbol, code| | |
define_method "render_#{symbol}" do |*args| | |
respond_to do |format| | |
format.html{render_error_html_file(code, (args.first || {}))} | |
format.all{head symbol} | |
end | |
true | |
end | |
end | |
# Render an html file under public/<Status code>.html, or default to just | |
# sending the status as a header. | |
def render_error_html_file code, options={} | |
file = File.join(RAILS_ROOT, 'public', "#{code}.html") | |
if File.exist? file | |
render options.reverse_merge(:file => file, :layout => false, :status => code) | |
else | |
head code | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment