Rails5.1.7でアプリを制作中です。
Strong Parametersが例外ActionController::ParameterMissingを発生させた場合に「400 Bad Request」のエラーページを表示させたいのですが,
ActionController::RoutingError (uninitialized constant ErrorHandlers::Forbidden) が発生してしまい上手くいきませんので、ご教授お願いします。
ErrorHandlersモジュールに例外処理メソッドrescue400を作り、それをクラスメソッドrescue_fromで例外ハンドラとして登録しました。
app/controllers/concerns/error_handlers.rb module ErrorHandlers extend ActiveSupport::Concern included do rescue_from Exception, with: :rescue500 rescue_from ActionController::ParameterMissing, with: :rescue400 rescue_from Forbidden, with: :rescue403 rescue_from IpAddressRejected, with: :rescue403 rescue_from ActionController::RoutingError, with: :rescue404 rescue_from ActiveRecord::RecordNotFound, with: :rescue404 end private def rescue400(e) @exception = e render 'errors/bad_request', status: 400 end def rescue403(e) @exception = e render 'errors/forbidden', status: 403 end def rescue404(e) @exception = e render 'errors/not_found', status: 404 end def rescue500(e) @exception = e render 'errors/internal_server_error', status: 500 end end
次にエラーページ用のERBテンプレートを作成しました。
app/views/errors/bad_request.html.erb <div id="error"> <h1>400 Bad Request</h1> <p>不正な要求です。</p> </div>
ErrorHandlersモジュールがApplicationControllerにincludeされるようにします。
app/controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery with: :exception layout :set_layout class Forbidden < ActionController::ActionControllerError; end class IpAddressRejected < ActionController::ActionControllerError; end include ErrorHandlers private def set_layout if params[:controller].match(%r{\A(admin|client|general)/}) Regexp.last_match[1] else 'client' end end end
config/routes.rb Rails.application.routes.draw do config = Rails.application.config.hunter constraints host: config[:client][:host] do namespace :client, path: config[:client][:path] do root 'top#index' get 'login' => 'sessions#new', as: :login resource :session, only: [ :create, :destroy ] resource :account, except: [ :new, :create, :destroy ] end end constraints host: config[:admin][:host] do namespace :admin, path: config[:admin][:path] do root 'top#index' get 'login' => 'sessions#new', as: :login resource :session, only: [ :create, :destroy ] resources :clients end end constraints host: config[:general][:host] do namespace :general, path: config[:general][:path] do root 'top#index' end end root 'errors#routing_error' get '*anything' => 'errors#routing_error' end
このようにしています。
application_controller.rb の include ErrorHandlers のところでエラーが発生しているようなのです。
ちなみに include ErrorHandlers if Rails.env.production として 開発環境では読み込まないようにするとエラーは出ません。
コンソールには以下のように表示されます。
Started GET "/admin/clients" for 10.0.2.2 at 2019-06-17 00:33:11 +0900 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 (0.7ms) SET NAMES utf8 COLLATE utf8_general_ci, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 (0.5ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC ActionController::RoutingError (uninitialized constant ErrorHandlers::Forbidden): app/controllers/concerns/error_handlers.rb:7:in `block in <module:ErrorHandlers>' app/controllers/application_controller.rb:9:in `include' app/controllers/application_controller.rb:9:in `<class:ApplicationController>' app/controllers/application_controller.rb:1:in `<top (required)>' app/controllers/admin/base.rb:1:in `<top (required)>' app/controllers/admin/clients_controller.rb:1:in `<top (required)>'
どなたか、解決方法おしえていただけませんでしょうか?宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/17 01:36
2019/06/17 03:08
2019/06/17 04:14