次のエラーがあります。
ActionController::RoutingError (No route matches [GET] "/images/favicon.ico")
存在しないリンクの error404 ページを表示したい。
どうすればそれを達成できますか?
次のエラーがあります。
ActionController::RoutingError (No route matches [GET] "/images/favicon.ico")
存在しないリンクの error404 ページを表示したい。
どうすればそれを達成できますか?
以下application_controller.rb
を追加します。
# You want to get exceptions in development, but not in production.
unless Rails.application.config.consider_all_requests_local
rescue_from ActionController::RoutingError, with: -> { render_404 }
end
def render_404
respond_to do |format|
format.html { render template: 'errors/not_found', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
私は通常、次の例外も救出しますが、それはあなた次第です:
rescue_from ActionController::UnknownController, with: -> { render_404 }
rescue_from ActiveRecord::RecordNotFound, with: -> { render_404 }
エラー コントローラーを作成します。
class ErrorsController < ApplicationController
def error_404
render 'errors/not_found'
end
end
次にroutes.rb
unless Rails.application.config.consider_all_requests_local
# having created corresponding controller and action
get '*path', to: 'errors#error_404', via: :all
end
最後に、次の場所にnot_found.html.haml
(または使用するテンプレート エンジンを)作成し/views/errors/
ます。
%span 404
%br
Page Not Found
ファビコン画像をコピーするとapp/assets/images
うまくいきました。