简体   繁体   中英

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

I made some changes in one of the controllers in our 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.

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.

You can fix it in one of two ways:

  1. when defining the cancelled_mino_paid method, make sure it can receive 3 arguments
  2. only send 2 arguments when calling the cancelled_mino_paid method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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