从ruby 2.7更新到ruby 3.0.1后出现argumenterror

xyhw6mcr  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(399)

在将ruby更新到3.0.1之后,非常简单的代码无法执行app\u uninstalled\u job.rb

class AppUninstalledJob < ActiveJob::Base
  def perform(shop_domain:, webhook:)
    shop = Shop.find_by(shopify_domain: shop_domain)

带着错误

Error performing AppUninstalledJob (Job ID:***) from Async(default) in 0.18ms: ArgumentError (wrong number of arguments (given 1, expected 0; required keywords: shop_domain, webhook)):
.../app/jobs/app_uninstalled_job.rb:2:in `perform'

数据接收正确

Started POST "/webhooks/app_uninstalled" for 34.69.74.99 at 2021-07-20 04:44:31 +0000
Processing by ShopifyApp::WebhooksController#receive as */*
  Parameters: {"id"=>876876876, "name"=>"shopname", "email"=>"***@gmail.com", "domain"=>"shop.myshopify.com", "province"=>.....}}
[ActiveJob] Enqueued AppUninstalledJob (Job ID: ) to Async(default) with arguments: {:shop_domain=>"shop.myshopify.com", :webhook=>{"id"=>876876876, "name"=>"shop", "email"=>"***@gmail.com", "domain"=>"shop.myshopify.com", "province"=>....}

[ActiveJob] [AppUninstalledJob] [****9] Performing AppUninstalledJob (Job ID:**99) from Async(default) enqueued at 2021-07-20T04:44:31Z with arguments: {:shop_domain=>"shop.myshopify.com", :webhook=>{"id"=>876876876, "name"=>"shop", "email"=>"***@gmail.com", "domain"=>"shop.myshopify.com", "province"....}
Completed 200 OK in 6ms (ActiveRecord: 0.0ms | Allocations: 2327)

它与处理参数委托有关https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
如何解决错误argumenterror(参数数目错误)(给定1,预期为0;所需关键词:商店(域、webhook)
更新:webhook控制器的代码

module ShopifyApp
  class MissingWebhookJobError < StandardError; end

  class WebhooksController < ActionController::Base
    include ShopifyApp::WebhookVerification

    def receive
      params.permit!
      job_args = { shop_domain: shop_domain, webhook: webhook_params.to_h }
      webhook_job_klass.perform_later(job_args)
      head(:ok)
    end

    private

    def webhook_params
      params.except(:controller, :action, :type)
    end

    def webhook_job_klass
      webhook_job_klass_name.safe_constantize || raise(ShopifyApp::MissingWebhookJobError)
    end

    def webhook_job_klass_name(type = webhook_type)
      [webhook_namespace, "#{type}_job"].compact.join('/').classify
    end

    def webhook_type
      params[:type]
    end

    def webhook_namespace
      ShopifyApp.configuration.webhook_jobs_namespace
    end
  end
end
s2j5cfk0

s2j5cfk01#

由于您已经分享了关于ruby 3中的更改的链接,答案就在链接中:
ruby 3.0中位置参数和关键字参数的分离:
在大多数情况下,可以通过添加double-splat操作符来避免不兼容。它显式指定传递关键字参数,而不是哈希对象。同样,可以添加大括号{}来显式传递哈希对象,而不是关键字参数。
现在看看您的代码,您正在执行以下操作:

job_args = { shop_domain: shop_domain, webhook: webhook_params.to_h }
webhook_job_klass.perform_later(job_args)

i、 例如,将散列传递给方法,而不是传递关键字参数,您可以通过添加双splat运算符来修复这些参数:

webhook_job_klass.perform_later(**job_args)

有关错误的详细信息:
错误是:

ArgumentError (wrong number of arguments (given 1, expected 0; required keywords: shop_domain, webhook)):

也就是说 shop_domainwebhook 是必需的关键字参数,并且您正在传递单个参数,因为ruby现在将哈希作为单个参数而不是关键字参数处理,直到添加双splat运算符为止。

相关问题