LoginSignup
4
4

More than 5 years have passed since last update.

Railsで動的にエラーページを表示する

Posted at

Railsでエラーページを表示する方法についてメモ。
rexceptionというGemを使う。

手順

まず、サンプルアプリケーションを生成する。

$ rails new sample

rexceptionを追加する。

$ vi Gemfile

gem 'rexception'

インストールする。

$ bundle

app/views下にerrorsディレクトリを作成する。
その下にapplication.html.erbを作成する。

app/views/errors/application.html.erb
<h1>Application Error</h1>

development環境でエラーを確認するために以下の設定をする。

config/environments/development.rb
config.consider_all_requests_local = false

WEBrickを起動する。

$ bin/rails s

存在しないURLにアクセスしてみる。

Screen_Shot_2015-01-19_at_5_53_32_PM.png

ログは以下のようになる。

Processing by Rexception::ExceptionsController#not_found as HTML
  Rendered errors/application.html.erb within layouts/application (0.2ms)
Completed 404 Not Found in 22ms (Views: 21.7ms | ActiveRecord: 0.0ms)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4