Я использую геокод.Идея заключается в том, что наши партнеры могут размещать товары с адресом.Когда они это делают, он выбирает широту и долготу.Теперь, когда наши клиенты идут покупать этот продукт, они должны ввести адрес доставки, чтобы сообщить нам, куда доставить продукт.Однако, если адрес доставки не находится в пределах 20 миль от товара, им не разрешается доставлять товар.
Я получаю сообщение об ошибке, говорящее об этом "неопределенном методе" широта "для nil: NilClass"
Как я уже сказал, product.longitude, product.latitude уже установлен, когда пользователи пытаютсяorder.
Не уверен, что это потому, что order.delivery_address (lat, long) еще не отправлен в базу данных и пытается проверить расстояние.Вот мой код ниже
Итак, мой вопрос: как я могу узнать, как я могу найти расстояние между адресом продукта и адресом заказа, и я хочу показать предупреждение пользователю, если расстояние между ними превышает 20миль.
def create
product = Product.find(params[:product_id])
if current_user == product.user
flash[:alert] = "You cannot purchase your own property"
elsif current_user.stripe_id.blank? || current_user.phone_number.blank?
flash[:alert] = " Please update your payment method and verify phone number please"
return redirect_to payment_method_path
elsif Geocoder::Calculations.distance_between([product.latitude, product.longitude], [@order.latitude, @order.longitude]) < 20
flash[:alert] = "The delivery address you provided is outside the delivery zone. Please choose a different product."
else
quantity = order_params[:quantity].to_i
@order = current_user.orders.build(order_params)
@order.product = product
@order.price = product.price
@order.total = product.price * quantity + product.delivery_price
# @order.save
if @order.Waiting!
if product.Request?
flash[:notice] = "Request sent successfully... Sit back and relax while our licensed dispensary fulfil your order :)"
else
@order.Approved!
flash[:notice] = "Your order is being filled and it will delivered shortly:)"
end
else
flash[:alert] = "Our licensed dispensary cannot fulfil your order at this time :( "
end
end
redirect_to product
end