title: Dynamic error pages in Rails 3
body: <p>In Rails 2.3.x, you can override <code>render_optional_error_file</code> like so:</p>
  
  <pre><code># ApplicationController.rb
  protected
    def render_optional_error_file(status_code)
      render :template =&gt; "errors/500", :status =&gt; 500, :layout =&gt; 'application'
    end
  </code></pre>
  
  <p>However, Rails 3 no longer has <code>render_optional_error_file</code>. Instead, you need to override <code>rescue_action_in_public</code>, which you can do like so:</p>
  
  <pre><code># config/initializers/error_page.rb
  module ActionDispatch
    class ShowExceptions
  
      protected    
        def rescue_action_in_public(exception)
          status = status_code(exception).to_s
  
          template = ActionView::Base.new(["#{Rails.root}/app/views"])
          if ["404"].include?(status)
            file = "/errors/404.html.erb"
          else
            file = "/errors/500.html.erb"
          end        
          body = template.render(:file =&gt; file)
  
          render(status, body)
        end
  
    end
  end
  </code></pre>
  
  <p>This works, but does not use the application's layout. However, if you specify the layout path like so:</p>
  
  <pre><code>body = template.render(:file =&gt; file, :layout =&gt; "layouts/application") # line 15
  </code></pre>
  
  <p>You get an <code>Error during failsafe response: ActionView::Template::Error</code>. </p>
  
  <p>Line 4 of application.html.erb:4 is:</p>
  
  <pre><code>&lt;%= stylesheet_link_tag "app", "jquery-ui", "jquery.fancybox", :cache =&gt; "all" %&gt;
  </code></pre>
  
  <p>Whatever ActionView normally uses to render templates isn't getting loaded.</p>
  
  <p>The stack trace is:</p>
  
  <pre><code>  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `join'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `rails_asset_id'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:817:in `rewrite_asset_path'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:746:in `compute_public_path'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:424:in `path_to_stylesheet'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:875:in `ensure_stylesheet_sources!'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `each'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `ensure_stylesheet_sources!'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:512:in `stylesheet_link_tag'
    /data/sites/fundraisers-stage/releases/20110316194843/app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___19482063_70294907435920_0'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:135:in `send'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:135:in `render'
    /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:54:in `instrument'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:127:in `render'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/layouts.rb:80:in `_render_layout'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:62:in `_render_template'
    /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:52:in `instrument'
    /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:52:in `instrument'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:56:in `_render_template'
    /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:26:in `render'
    /data/sites/fundraisers-stage/releases/20110316194843/config/initializers/error_pages.rb:15:in `rescue_action_in_public'
  </code></pre>