Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Static Pages in Ruby on Rails

Jamie Barton
Jamie Barton
14,498 Points

A better way to render 404's

I'd recommend adding layout: false to the render 404 method.

render file: 'public/404.html', status: :not_found, layout: false

2 Answers

If you're not relying on any custom logic to determine when a not found should be rendered — that is if it's purely based on records not being found in the database — another option is to use rescue_from in your ApplicationController and allow your other controllers to raise ActiveRecord::RecordNotFound. Just saves you the step of having to catch those exceptions and render in each controller action. Though you may have already been doing this in which case i'm just being redundant. :P

application_controller.rb
class ApplicationController < ActionController::Base
  rescue_from ActiveRecord::RecordNotFound, with: :not_found

  private
  def not_found
    render file: 'public/404.html', status: :not_found, layout: false
  end
end
Jamie Barton
Jamie Barton
14,498 Points

Sorry this was meant to be feedback for the video. I appear to have posted it on the forum

No worries, the forums are a fine place to discuss video content as well.