简体   繁体   English

Ruby web应用程序,得到“错误数量的参数(给定3,预期2)”错误

[英]Ruby webapplication, getting "wrong number of arguments (given 3, expected 2)" error

I made some changes in one of the controllers in our webapp.我对我们 webapp 中的一个控制器进行了一些更改。 Essentially, the controller was sending out an email to the customer if the order was cancelled, and changed the status of the order in the database.本质上,如果订单被取消,控制器会向客户发送一封电子邮件,并更改数据库中订单的状态。 This is how it originally the snippet looked like:这是片段最初的样子:

  elsif @ac == "mino"
    begin
      @wifi_order = WifiOrder.find(params["id"])
      ApplicationMailer.cancelled_mino(@wifi_order, WifiUser.find_by(email: @wifi_order.email), 10000).deliver_now
      @wifi_order = WifiOrder.find(params["id"])
      @wifi_order.order_status = "status_cancelled_pending_fees"
      @wifi_order.order_status_sub = "status_cancelled_force"
      @wifi_order.cancelled_at = DateTime.now
      @wifi_order.payment_next = nil 
      @wifi_order.confirm = nil 
      @wifi_order.save!
    rescue => e
      p e.message
      flash[:error] = e.message 
      return
    end 

This is what I changed it for, because I wanted to send two different kinds of emails depending on the payment method set:这就是我改变它的原因,因为我想根据付款方式设置发送两种不同类型的电子邮件:

  elsif @ac == "mino"
    begin
      @wifi_order = WifiOrder.find(params["id"])
      if @wifi_order.pay_type = "card"
        ApplicationMailer.cancelled_mino(@wifi_order, WifiUser.find_by(email: @wifi_order.email), 10000).deliver_now
      else
        ApplicationMailer.cancelled_mino_paid(@wifi_order, WifiUser.find_by(email: @wifi_order.email), 10000).deliver_now 
      end
      @wifi_order = WifiOrder.find(params["id"])
      @wifi_order.order_status = "status_cancelled_pending_fees"
      @wifi_order.order_status_sub = "status_cancelled_force"
      @wifi_order.cancelled_at = DateTime.now
      @wifi_order.payment_next = nil 
      @wifi_order.confirm = nil 
      @wifi_order.save!
    rescue => e
      p e.message
      flash[:error] = e.message 
      return
    end  

Ever since the changes, when I try to test it, I get this error:自从更改以来,当我尝试对其进行测试时,出现此错误:

wrong number of arguments (given 3, expected 2)

What did I do wrong?我做错了什么?

Why it is happening为什么会发生

The wrong number of arguments you are getting hints at the solution.您得到解决方案提示的参数数量错误。

It details a method is expecting 2 arguments while you are sending 3.它详细说明了一个方法在您发送 3 个参数时需要 2 个参数。

def say_hello(first_name, last_name)
 puts "#{first_name} #{last_name}"
end

say_hello("John", "Doe") # -> will work
say_hello("John", "Von", "Doe") # -> will raise wrong number of arguments error

How to fix it如何修复它

At first sight, it seems that the method you've added cancelled_mino_paid is the cause of the wrong number of arguments error.乍一看,您添加的方法cancelled_mino_paid似乎是错误数量参数错误的原因。

You can fix it in one of two ways:您可以通过以下两种方式之一修复它:

  1. when defining the cancelled_mino_paid method, make sure it can receive 3 arguments在定义cancelled_mino_paid方法时,确保它可以接收 3 个参数
  2. only send 2 arguments when calling the cancelled_mino_paid method调用cancelled_mino_paid方法时只发送 2 个参数

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM