需要一个处理程序。传递 with: 关键字参数或提供一个块

问题描述 投票:0回答:2

最近我将我的应用程序从 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>'

我刚刚阅读了 Here 关于导致第 56 行错误的函数。

applciation_controller.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

我认为这是因为折旧。

谁能告诉我如何处理这些错误?

ruby-on-rails ruby ruby-on-rails-6 rbenv ruby-3
2个回答
5
投票

您的

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

----- 更新

这是由于您使用的

will_paginate
gem 覆盖了控制器中的
rescue_from
方法,同时新的 ruby 更新改变了 围绕关键字属性的行为

如果您的基本控制器

include ControllerRescuePatch
您可能可以将其删除,这将修复它,但不确定您的分页会发生什么。 否则,请推迟 ruby 更新,直到
will_paginate
更新其代码来修复此问题


0
投票

我只是尝试使用 will_paginate gem 的 Letest 版本,它对我有用

gem 'will_paginate', '~> 4.0'
© www.soinside.com 2019 - 2024. All rights reserved.