简体   繁体   English

升级到 Rails 6 后参数数量错误(给定 4,预期为 0..1)

[英]wrong number of arguments (given 4, expected 0..1) after upgrading to Rails 6

I just upgraded from Rails 5.2 to Rails 6 and I'm facing an issue in one class.我刚刚从 Rails 5.2 升级到 Rails 6,我在一个课程中遇到了一个问题。

class Calculator < ApplicationRecord
  def initialize(obj, user_id, calc_type, will_id = nil )
    @inheritors = obj
    super_obj = obj.clone.merge!(user_id: user_id, type: calc_type, will_id: will_id)
    super(super_obj)
    @shares = {}
    @remains = RationalWithArgumentStore(0)
    @stop_residual_shares = false
    @special_case_apply = false
    @rules = {}
    @authorities = {}
  end
end

and I'm creating new instance of the class like the following我正在创建类的新实例,如下所示

calc = Calculator.new(obj, user_id, calc_type, nil)

It through an error saying:它通过一个错误说:

wrong number of arguments (given 4, expected 0..1)

I spent two days searching and trying to fix the issue without any luck.我花了两天时间搜索并试图解决这个问题,但没有任何运气。 Your help is highly appreciated非常感谢您的帮助

This is happening because you are redefining initialize for a class that inherits from ApplicationRecord .发生这种情况是因为您正在为继承自ApplicationRecord的类重新定义initialize It throws an error here .在这里抛出一个错误。 If you were to do the following it would work.如果您要执行以下操作,它将起作用。

class Calculator
  def initialize(obj, user_id, calc_type, will_id = nil)
    @obj = obj
    @user_id = user_id
    @calc_type = calc_type
    @will_id = will_id
  end
end

Note that redefining initialize in this way is not recommended (see docs here ), so if you can I would look into callbacks and see if you can accomplish your goal with that.请注意,不建议以这种方式重新定义initialize (请参阅此处的文档),因此,如果可以,我会研究回调,看看您是否可以用它来实现您的目标。

Hope that helps.希望有帮助。

(Rails 6.x) One of my models had include Rails.application.routes.url_helpers . (Rails 6.x)我的一个模型include Rails.application.routes.url_helpers

If you have the same problem, remove it and manage your url methods by calling it directly as:如果您有同样的问题,请将其删除并通过直接调用它来管理您的 url 方法:

def my_link_function
 # ... my code
 my_link = Rails.application.routes.url_helpers.url_for([my_params]))
 # ... other code
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails - Geocoder - 。参数数量错误(给定3,预期0..1) - Rails - Geocoder - .near wrong number of arguments (given 3, expected 0..1) Rails:ActionView::Template::Error(参数数量错误(给定 4,预期 0..1)): - Rails: ActionView::Template::Error (wrong number of arguments (given 4, expected 0..1)): 在Rails中导入时,参数数目错误(给定2,应为0..1) - wrong number of arguments (given 2, expected 0..1) while importing in Rails ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1) ArgumentError-参数数量错误(给定2个,预期值为0..1):排序问题Rails 4.2 - ArgumentError - wrong number of arguments (given 2, expected 0..1): sorting issue Rails 4.2 错误.full_messages | arguments 的错误编号(给定 2,预期 0..1) - errors.full_messages | wrong number of arguments (given 2, expected 0..1) Ruby ArgumentError:arguments 的错误编号(给定 2,预期 0..1) - Ruby ArgumentError: wrong number of arguments (given 2, expected 0..1) Rails - 错误的参数数量(2为0..1)错误 - Rails - wrong number of arguments (2 for 0..1) error Rails:参数数量错误(给定 2,预期为 1)MongoID - Rails: Wrong number of arguments (given 2, expected 1) MongoID Rails:参数数量错误(给定1,预期为0) - Rails: Wrong number of arguments(given 1, expected 0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM