Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Custom error pages in Rails
Ayush Newatia
Ayush Newatia

Posted on

     

Custom error pages in Rails

This post was extracted and adapted fromThe Rails and Hotwire Codex.

When something goes wrong in Rails, the user sees a rather boring default error page.

The default Rails 404 error page

This page lives in the/public folder and hence isn't rendered through the Rails stack.

To jazz up this page a bit, we'll create a controller to render errors so the Rails infrastructure can be used.

Setting up

A configuration change needs to be made so public facing errors are rendered in development rather than the exception and stack trace.

# config/environments/development.rbrequire"active_support/core_ext/integer/time"Rails.application.configuredo# ...config.consider_all_requests_local=falseend
Enter fullscreen modeExit fullscreen mode

Create the controller.

$ bin/rails g controller errors --no-helper --no-test-framework
Enter fullscreen modeExit fullscreen mode

This controller will have a singleshow action where we'll extract the error code for the raised exception and render the appropriate view. Error codes403,404 and500 will have dedicated error pages and we'll fall back to the404 page for everything else.

classErrorsController<ApplicationControllerlayout"error"defshow@exception=request.env["action_dispatch.exception"]@status_code=@exception.try(:status_code)||ActionDispatch::ExceptionWrapper.new(request.env,@exception).status_coderenderview_for_code(@status_code),status:@status_codeendprivatedefview_for_code(code)supported_error_codes.fetch(code,"404")enddefsupported_error_codes{403=>"403",404=>"404",500=>"500"}endend
Enter fullscreen modeExit fullscreen mode

As you can see, we're using a bespoke"error" layout as well. These pages will be simple and won't need the usual baggage from the application layout.

Create the new error layout.

$ touch app/views/layouts/error.html.erb
Enter fullscreen modeExit fullscreen mode
<%# app/views/layouts/error.html.erb %><!DOCTYPE html><html><head><%=render"layouts/head"%></head><body><main><%=yield%></main></body></html>
Enter fullscreen modeExit fullscreen mode

Create the views and fill them in as you wish.

$ touch app/views/errors/403.html.erb$ touch app/views/errors/404.html.erb$ touch app/views/errors/500.html.erb
Enter fullscreen modeExit fullscreen mode

We need to now tell Rails to use this controller to render errors.

Exceptions app

Rails provides a hook to render custom errors through a configuration property calledexceptions_app. This property has to be assigned aRack app which is invoked when an exception is raised.

Every Rails controller action is actually its own Rack application! The Rack endpoint is returned by theaction method on a controller class.

# ...moduleMyAppclassApplication<Rails::Applicationconfig.load_defaults7.0config.exceptions_app=->(env){ErrorsController.action(:show).call(env)}endend
Enter fullscreen modeExit fullscreen mode

Restart your server and then try visiting a page that doesn't exist. Or manually trigger an error in a controller action. You'll see your custom error pages in action!

If you liked this post, check out my book,The Rails and Hotwire Codex, to level-up your Rails and Hotwire skills!

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
shalvah profile image
Shalvah
Builder, explorer, writer. APIs, dev tools, automation. Advocate of simple design. I blog at http://blog.shalvah.me
  • Location
    Lagos, Nigeria
  • Work
    Software Engineer
  • Joined

Extremely helpful, thanks!

CollapseExpand
 
gathuku profile image
Moses Gathuku
Rubyist, Ruby on Rails Software Developer
  • Location
    Nairobi
  • Education
    Bsc Computer Science
  • Work
    Full Stack Ruby on Rails at Freelance
  • Joined

This is great@ayushn21 !

CollapseExpand
 
gathuku profile image
Moses Gathuku
Rubyist, Ruby on Rails Software Developer
  • Location
    Nairobi
  • Education
    Bsc Computer Science
  • Work
    Full Stack Ruby on Rails at Freelance
  • Joined
• Edited on• Edited

@ayushn21 I noticed this doesn't work withActionController::InvalidAuthenticityToken look like it's a problem when a request is in a POST method.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a freelance web developer and Rubyist. I'm the author of The Rails and Hotwire Codex (https://railsandhotwirecodex.com) and the co-host of Just A Spec.
  • Location
    London, UK
  • Joined

More fromAyush Newatia

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp