Skip to content

Instantly share code, notes, and snippets.

@heisters
Created May 5, 2009 17:02
Show Gist options
  • Save heisters/107057 to your computer and use it in GitHub Desktop.
Save heisters/107057 to your computer and use it in GitHub Desktop.
automatically define Rails render_* methods for all possible HTTP responses
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