Techioz Blog

ハンドラーが必要です。 with: キーワード引数を渡すか、ブロックを指定します

概要

最近、アプリを Ruby バージョン 2.6.1 から 3.0.1 に更新し、バージョン マネージャーとして rbenv を使用しています。

しかし、Railsサーバーを実行しようとするとエラーが発生しました

=> Booting Puma
=> Rails 6.1.3 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/home/humayun/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3/lib/active_support/rescuable.rb:56:in `rescue_from': Need a handler. Pass the with: keyword argument or provide a block. (ArgumentError)
    from /home/humayun/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/will_paginate-3.1.8/lib/will_paginate/railtie.rb:67:in `rescue_from'
    from /home/humayun/umerfarooq/Alchemy/app/controllers/application_controller.rb:2:in `<class:ApplicationController>'
    from /home/humayun/umerfarooq/Alchemy/app/controllers/application_controller.rb:1:in `<main>'

ここで、56 行目でエラーを引き起こしている関数について読みました。

アプリケーションコントローラー.rb

 rescue_from Exception, with: :handle_exception
 protect_from_forgery prepend: true, with: :exception
 before_action :configure_permitted_parameters, if: :devise_controller?
 before_action :initialize_api

  def not_found
    raise ActionController::RoutingError.new('Not Found')
  end

 def handle_exception(exception = nil)
    return render_404 if [ActionController::RoutingError, ActiveRecord::RecordNotFound].include?(exception.class)
     render_500
 end

減価償却のせいだと思います。

誰かこれらのエラーを処理する方法を教えてください。

解決策

handle_Exception には、ビューをレンダリングするかステータスを返すブロックが必要になる可能性があります。

app/controllers/application_controller.rb:2 のエラーで述べたように、エラーやハンドラーのないrescue_fromがある可能性があります。以下の構文のいずれかに従う必要があります。

class ApplicationController < ActionController::Base
  rescue_from User::NotAuthorized, with: :deny_access # self defined exception
  rescue_from ActiveRecord::RecordInvalid, with: :show_errors

  rescue_from 'MyAppError::Base' do |exception|
    render xml: exception, status: 500
  end

  private
    def deny_access
      ...
    end

    def show_errors(exception)
      exception.record.new_record? ? ...
    end
end

ここのドキュメントに従って https://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from

        • アップデート:

これは、キーワード属性に関する動作を変更する新しい Ruby の更新とともに、コントローラーのレスキュー_フロム メソッドをオーバーライドする、使用している will_paginate gem が原因です。

ベースコントローラーにControllerRescuePatchが含まれている場合は、それを削除できる可能性があり、これで問題は解決しますが、ページネーションに何が起こるかはわかりません。 それ以外の場合は、will_paginate がコードを更新してこれを修正するまで Ruby の更新を保留します。