更新方法的参数数量错误(1 对 2)
Wrong number of arguments (1 for 2) for update method
我在追踪为什么我的更新方法没有获得所需参数时遇到了问题。我对 show 进行了类似的测试,有效载荷正在运行。在这种情况下,有问题的路线是 invoice/invoice_id/trip/id。如果您能帮我找出错误并就今后如何解决此类问题提供任何建议,那就太好了。
这是更新方法。
def update
if @trip.update(@trip.trip_id, trip_params)
head :no_content
else
render json: [@invoice, @trip].errors, status: :unprocessable_entity
end
end
使用以下私有方法。
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
我失败的测试是这样的。
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
如果您在 before_action
中调用 set_trip
,那么 update()
方法应该如下所示
def update
if @trip.update(trip_params)
head :no_content
else
render json: [@invoice, @trip].errors, status: :unprocessable_entity
end
end
update()
是一个可以使用object调用的实例方法,只需要传trip_params
进去,希望对你有帮助!
当该方法调用另一个传递了错误数量参数的方法时,您会收到此错误消息。
update 将散列作为其唯一参数,但您在更新方法中传递了两个参数(@trip.trip_id、trip_params)。这就是您收到 "Wrong number of arguments (1 for 2) for update method" 错误消息的原因。正如@RSB所说,只需传入 trip_params , Trip 实例就会更新。
RSB 是对的。事实证明,在这种情况下,我的问题出在数据库级别。 table 没有主键,所以我使用
@trip = Trip.where
在私有方法中,这导致它返回一组可能的行而不是特定的行。我在数据库级别进行了更改以拥有主键并更新了私有方法。瞧,RSB 的代码成功了!
我在追踪为什么我的更新方法没有获得所需参数时遇到了问题。我对 show 进行了类似的测试,有效载荷正在运行。在这种情况下,有问题的路线是 invoice/invoice_id/trip/id。如果您能帮我找出错误并就今后如何解决此类问题提供任何建议,那就太好了。
这是更新方法。
def update
if @trip.update(@trip.trip_id, trip_params)
head :no_content
else
render json: [@invoice, @trip].errors, status: :unprocessable_entity
end
end
使用以下私有方法。
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
我失败的测试是这样的。
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
如果您在 before_action
中调用 set_trip
,那么 update()
方法应该如下所示
def update
if @trip.update(trip_params)
head :no_content
else
render json: [@invoice, @trip].errors, status: :unprocessable_entity
end
end
update()
是一个可以使用object调用的实例方法,只需要传trip_params
进去,希望对你有帮助!
当该方法调用另一个传递了错误数量参数的方法时,您会收到此错误消息。
update 将散列作为其唯一参数,但您在更新方法中传递了两个参数(@trip.trip_id、trip_params)。这就是您收到 "Wrong number of arguments (1 for 2) for update method" 错误消息的原因。正如@RSB所说,只需传入 trip_params , Trip 实例就会更新。
RSB 是对的。事实证明,在这种情况下,我的问题出在数据库级别。 table 没有主键,所以我使用
@trip = Trip.where
在私有方法中,这导致它返回一组可能的行而不是特定的行。我在数据库级别进行了更改以拥有主键并更新了私有方法。瞧,RSB 的代码成功了!