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.

On Wed, Jan 21, 2009 at 10:09 PM, Dowker [email protected] wrote:

protected
=> :page_not_found

Thanks.

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


Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain

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:inrecognize’
/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:indispatch_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:insynchronize’
/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:indispatch_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:inprocess’
/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:inprocess’
/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:ineach’
/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:inrun’
/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:innew’
/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:ininitialize’
/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:inrun’
/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:ineach’
/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:inrun’
/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:inload_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:innew_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:ingem_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:inrequire’
/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:inrequire’
/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:inrequire’
./script/server:3

Thanks.

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.

On Jan 22, 10:06 am, Andrew T. [email protected]

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

On Jan 22, 10:06 am, Andrew T. [email protected]

On Thu, Jan 22, 2009 at 4:11 PM, Dowker [email protected] wrote:

09:05:10) [GET]
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/ mongrel.rb:158: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' activesupport-2.2.2/lib/active_support/dependencies.rb:142:in load’
custom_require.rb:31:in require' 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


Andrew T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

“I have never let my schooling interfere with my education” - Mark Twain