Rendering layout when using rescue_from

The following exception handlers work fine. It displays a simple page with the text "Page Not Found.". But I would like to render the message within the application layout.

class ApplicationController < ActionController::Base   rescue_from ActionController::RoutingError, :with => :page_not_found   rescue_from ActionController::UnknownAction, :with => :page_not_found

  protected     def page_not_found       render :text => 'Page Not Found.', :status => :not_found     end end

Therefore when I add the layout option to the render method, as such:

class ApplicationController < ActionController::Base   rescue_from ActionController::RoutingError, :with => :page_not_found   rescue_from ActionController::UnknownAction, :with => :page_not_found

  protected     def page_not_found       render :text => 'Page Not Found.', :layout => true, :status => :not_found     end end

The response generates a "500 Internal Server Error" error. Can anybody shed some light on why this is happening?

Thanks.

You will need to specify which layout to use: render :text → ‘Page Not Found’, :layout => ‘application’, :status => :not_found

Thanks Andrew. I tried your suggestion and changed the render to:

render :text => 'Page Not Found.', :layout => 'application', :status => :not_found

I'm still getting a "500 Internal Server Error" error. Here's the console output which may help:

Processing ApplicationController#index (for 127.0.0.1 at 2009-01-22 09:05:10) [GET] Rendering template within layouts/application /!\ FAILSAFE /!\ Thu Jan 22 09:05:10 -0500 2009   Status: 500 Internal Server Error   No route matches "/dd" with {:method=>:get}     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/routing/recognition_optimisation.rb:66:in `recognize_path'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/routing/route_set.rb:386:in `recognize'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:182:in `handle_request'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:110:in `dispatch_unlocked'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:123:in `dispatch'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:122:in `synchronize'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:122:in `dispatch'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:132:in `dispatch_cgi'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/actionpack-2.2.2/ lib/action_controller/dispatcher.rb:39:in `dispatch'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/ bin/../lib/mongrel/rails.rb:76:in `process'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/ bin/../lib/mongrel/rails.rb:74:in `synchronize'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/ bin/../lib/mongrel/rails.rb:74:in `process'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:159:in `process_client'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:158:in `each'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:158:in `process_client'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:285:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:285:in `initialize'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:285:in `new'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:285:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:268:in `initialize'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:268:in `new'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel.rb:268:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel/configurator.rb:282:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel/configurator.rb:281:in `each'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel/configurator.rb:281:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/ mongrel_rails:128:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/ mongrel/command.rb:212:in `run'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/ mongrel_rails:281     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:142:in `load_without_new_constant_marking'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:142:in `load'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:521:in `new_constants_in'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:142:in `load'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/ commands/servers/mongrel.rb:64     /usr/local/ruby-1.8.7-p72/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:31:in `gem_original_require'     /usr/local/ruby-1.8.7-p72/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:31:in `require'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:153:in `require'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:521:in `new_constants_in'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/ activesupport-2.2.2/lib/active_support/dependencies.rb:153:in `require'     /usr/local/ruby-1.8.7-p72/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/ commands/server.rb:49     /usr/local/ruby-1.8.7-p72/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:31:in `gem_original_require'     /usr/local/ruby-1.8.7-p72/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:31:in `require'     ./script/server:3

Thanks.

Here’s the code straight out of my application controller:

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

private def record_not_found

render :template => 'not_found', :layout => 'error', :status => :not_found

end

I got it working by simplifying the code and isolating the problem. The error happens when I use my regular layout application.html.erb. If I remove all the class variables used throughout the layout, it works fine. My goal was to use my standard layout (navigation, etc.) with the error message. Why would this error occur? Thanks.

I figured out the problem. My before_filters in ApplicationController are not running before the rescue_from exception handlers. What's the best way to handle this? Do I have to explicitly call each method that sets up the environment like this?

private   def page_not_found     set_locale # Method declared in ApplicationController     initialize_page_attributes # Method declared in ApplicationController     render :template => 'errors/page_not_found', :layout => true, :status => :not_found   end