CheeseFry
CheeseFry

Reputation: 1319

Wrong number of arguments (1 for 2) for update method

I'm having an issue tracking down why my update method isn't getting the needed arguments. I have a similar test for show and the payloads are working. In this scenario the route in question is invoice/invoice_id/trip/id. If you can help me spot the error and give any suggestions on how to troubleshoot this type of problem in the future that'd be great.

This is the update method.

def update
  if @trip.update(@trip.trip_id, trip_params)
    head :no_content
  else
    render json: [@invoice, @trip].errors, status: :unprocessable_entity
  end
end

With the following private methods.

private

  def set_trip
    @trip = Trip.where(:invoice_id => params[:invoice_id], :trip_id => params[:id] )
  end

  def trip_params
    params.require(:trip).permit(:trip_id, :depart_airport, :arrive_airport, :passenger_first_name, :passenger_last_name, :passenger_count, :departure_date, :merchant_id)
  end

  def load_invoice
    @invoice = Invoice.find(params[:invoice_id])
  end

end

My failing test looks like this.

test "should update trip" do
  put :update, invoice_id: @invoice.invoice_id, id: @trip,
   trip: {arrive_airport: @trip.arrive_airport,
   depart_airport: @trip.depart_airport,
   departure_date: @trip.departure_date,
   passenger_count: @trip.passenger_count,
   passenger_first_name: @trip.passenger_first_name, 
   passenger_last_name: @trip.passenger_last_name}
 assert_response 204
end

Upvotes: 1

Views: 1118

Answers (4)

CheeseFry
CheeseFry

Reputation: 1319

RSB was right on the money. It turned out in this case that my issue was at the database level. The table didn't have a primary key so I was using @trip = Trip.where in the private method and this was causing it to come back with an array of possible rows rather than the specific one. I changed things at the database level to have a primary key and updated the private method. Voilà RSB's code worked!

Upvotes: 0

mgidea
mgidea

Reputation: 494

update takes a hash as its one and only argument, but you are are passing two arguments (@trip.trip_id, trip_params) in the update method. This is why you are getting the "Wrong number of arguments (1 for 2) for update method" error message. as @RSB said, just pass in the trip_params and the Trip instance will be updated.

Upvotes: 1

Max N
Max N

Reputation: 81

You can get this error message when the method is calling another which is being passed the wrong number of arguments.

Upvotes: 1

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

if you are calling set_trip in before_action then update() method should look like this

def update
  if @trip.update(trip_params)
    head :no_content
  else
    render json: [@invoice, @trip].errors, status: :unprocessable_entity
  end
end

update() is an instance method that can be called using object, you only need to pass trip_params into it, Hope that helps!

Upvotes: 1

Related Questions