Never write a render_foo method again

[Authored by Ian]

I’ve probably written a hundred render_not_found methods in my life as a Rails dev. Usually they just render a static file under /public, and maybe, if I’m feeling nice, give an XML response. No more!

class ApplicationController < ActionController::Base   protected   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     end   end   def render_error_html_file code, options={}     render options.reverse_merge(       :file => File.join(RAILS_ROOT, 'public', "#{code}.html"),
      :layout => false,
      :status => code

This will give you every render_* method you’ve ever wanted, including the ever-useful render_im_used

Leave a Reply