Rendering a 404 page with a layout

Hello,

I want my 404 and 500 pages to use the application layout, so they look
like the rest of my site. So, I’m doing this

def rescue_action_in_public(exception)
    logger.error("rescue_action_in_public executed")
    case exception
    when ActiveRecord::RecordNotFound,
        ::ActionController::UnknownAction,
        ::ActionController::RoutingError
        render(:file => "#{RAILS_ROOT}/public/404.html",
               :layout => 'application',
               :status  => "404 Not Found")
        #SystemNotifier.deliver_exception_notification(
        #    self, request, exception)
    else
        render(:file => "#{RAILS_ROOT}/public/500.html",
               :layout => 'application',
               :status  => "500 Error")
        #SystemNotifier.deliver_exception_notification(
        #    self, request, exception)
    end
end

But, it only works at the root level. If you go to
http://www.pawsitiveapproach.ca/foobar, then you get the main layout on
the 404 page. But, if you go to
http://www.pawsitiveapproach.ca/articles/foobar, you don’t.

Is this just a routing issue? Here are my routes.

ActionController::Routing::Routes.draw do |map|

With no path provided, default to the front page.

map.connect ‘’, :controller => “main”, :action => ‘index’

Route for the admin section.

map.connect ‘/admin/petfacts/:action/:id’, :controller =>
‘admin_petfact’
map.connect ‘/admin/testimonials/:action/:id’, :controller =>
‘admin_testimonial’
map.connect ‘/admin/articles/:action/:id’, :controller =>
‘admin_article’
map.connect ‘/admin/users/:action/:id’, :controller => ‘admin_users’

map.connect ‘/admin/’, :controller => ‘admin’, :action => ‘index’

Map for the top-level, so we don’t see ‘main’ in the URLs.

map.connect ‘/:action/:id’,
:controller => “main”, :requirements => { :id => /^\d+$/ }

Standard route.

map.connect ‘:controller/:action/:id’
end

Also, how do you catch routing errors? I’m trying to in my exception
handler above, but it’s not working.

Thanks,
Mike

what version of Rails are you using? I’m running edge rails right now
(changeset 5533) and having a similar problem.

pjkelly wrote:

what version of Rails are you using? I’m running edge rails right now
(changeset 5533) and having a similar problem.

you can render 404 but not 500 if you get a 500 error your application
is not served.

just create an error controller with 404 action

I was doing the same thing msoulier is doing above by trying to catch
RecordNotFound and RoutingErrors with rescue_action_in_public but it
seems to be broken in Edge Rails. It worked fine in 1.1.6.

With the code below, is it now working in 1.2?
And where would this def go??

Blessed Be

Phillip

msoulier wrote:

Hello,

I want my 404 and 500 pages to use the application layout, so they look
like the rest of my site. So, I’m doing this

def rescue_action_in_public(exception)
    logger.error("rescue_action_in_public executed")
    case exception
    when ActiveRecord::RecordNotFound,
        ::ActionController::UnknownAction,
        ::ActionController::RoutingError
        render(:file => "#{RAILS_ROOT}/public/404.html",
               :layout => 'application',
               :status  => "404 Not Found")
        #SystemNotifier.deliver_exception_notification(
        #    self, request, exception)
    else
        render(:file => "#{RAILS_ROOT}/public/500.html",
               :layout => 'application',
               :status  => "500 Error")
        #SystemNotifier.deliver_exception_notification(
        #    self, request, exception)
    end
end

But, it only works at the root level. If you go to
http://www.pawsitiveapproach.ca/foobar, then you get the main layout on
the 404 page. But, if you go to
http://www.pawsitiveapproach.ca/articles/foobar, you don’t.