gpt4 book ai didi

mysql - AJAX 资源的复杂事件记录查询

转载 作者:行者123 更新时间:2023-11-29 09:56:03 25 4
gpt4 key购买 nike

我有一个国家/地区选择字段,第二个选择字段用于给定国家/地区的运输选项,当选择第一个国家/地区和第二个运输时,我想显示运费。

当我第一次加载运输车时,显示 Shippingservices 资源已正确映射到第一个国家/地区(仅特定重量和选定国家/地区的服务),但是当我对 update_shipping 操作使用相同的查询时我得到的ajax资源 postzones.land_id = NULL

我的 Controller :

class CartsController < ApplicationController

def show
@cart = Cart.find(params[:id])
@shippingservices = @cart.available_shipping_services.includes(:lands, :postzones).where('postzones.land_id = ?', Land.first.id)
end

def update_shipping
@cart = Cart.find(params[:id])
@shippingservices = @cart.available_shipping_services.includes(:lands, :postzones).where('postzones.land_id = ?', params[:land_id])
respond_to do |format|
format.js
end
end
end

在我的购物车模型中,我有:

def available_shipping_services
Shippingservice.where('weightmin <= ? and weightmax >= ?', total_weight, total_weight).where('shippingservices.shippingcarrier = ?', '1')
end

javascript AJAX 部分感谢@rewriting:

$(document).on("change", "#lands_select", function(event){
$.ajax({
url: "/carts/update_shipping/" + event.target.value,
type: "GET"
})
});

app/views/carts/update_shipping.js.erb 是:

$("#shippingservices_select").empty()
.append("<%= escape_javascript(render(:partial => @shippingservices)) %>")

app/views/carts/_shippingservices.html.erb 是:

<option value="<%= shippingservice.id %>"><%= shippingservice.name.titleize %></option>

当我尝试进行 ajax 调用时,我得到:

Started GET "/carts/update_shipping/2" for 127.0.0.1 at Tue Dec 18 14:17:21 +0100 2018
Processing by CartsController#update_shipping as */*
Parameters: {"id"=>"2"}
Cart Load (0.4ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1 [["id", 5]]
Cart Load (0.2ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1 [["id", "2"]]
CartItem Load (0.4ms) SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 2
SQL (0.9ms) SELECT `shippingservices`.`id` AS t0_r0, `shippingservices`.`serviceID` AS t0_r1, `shippingservices`.`title` AS t0_r2, `shippingservices`.`name` AS t0_r3, `shippingservices`.`shippingcarrier` AS t0_r4, `shippingservices`.`length` AS t0_r5, `shippingservices`.`width` AS t0_r6, `shippingservices`.`height` AS t0_r7, `shippingservices`.`weightmin` AS t0_r8, `shippingservices`.`weightmax` AS t0_r9, `shippingservices`.`price` AS t0_r10, `shippingservices`.`created_at` AS t0_r11, `shippingservices`.`updated_at` AS t0_r12, `lands`.`id` AS t1_r0, `lands`.`name` AS t1_r1, `lands`.`partof` AS t1_r2, `lands`.`continent` AS t1_r3, `lands`.`area` AS t1_r4, `lands`.`CallingCode` AS t1_r5, `lands`.`ISOcode2` AS t1_r6, `lands`.`ISOcode3` AS t1_r7, `lands`.`created_at` AS t1_r8, `lands`.`updated_at` AS t1_r9, `postzones_shippingservices`.`id` AS t2_r0, `postzones_shippingservices`.`land_id` AS t2_r1, `postzones_shippingservices`.`shippingservicezone` AS t2_r2, `postzones_shippingservices`.`shippingservice_id` AS t2_r3, `postzones_shippingservices`.`created_at` AS t2_r4, `postzones_shippingservices`.`updated_at` AS t2_r5 FROM `shippingservices` LEFT OUTER JOIN `postzones` ON `postzones`.`shippingservice_id` = `shippingservices`.`id` LEFT OUTER JOIN `lands` ON `lands`.`id` = `postzones`.`land_id` LEFT OUTER JOIN `postzones` `postzones_shippingservices` ON `postzones_shippingservices`.`shippingservice_id` = `shippingservices`.`id` WHERE (weightmin <= 0 and weightmax >= 0) AND (shippingservices.shippingcarrier = 'Poste italiane') AND (postzones.land_id = NULL)
Rendered collection (0.0ms)
Rendered carts/update_shipping.js (3.2ms)
Completed 200 OK in 13ms (Views: 7.6ms | ActiveRecord: 1.8ms)

我想一切都设置正确,但 Controller 无法解析事件记录查询,因为他寻找 (postzones.land_id = NULL)

我怎样才能得到类似的东西:postzones.land_id = 参数:{“id”=>“2”}。这意味着所有尊重 ID 为 2 的国家/地区的购物车重量的运输服务?

提前谢谢大家。

更新:

在我的 app/views/carts/show.html.erb 中,我有以下表单:

<%= form_for :cart, :url => {:action => "update_shipping"}, :html => { :method => "get", :remote => true } do |f| %>
<%= f.select(:land_id, options_for_select(@lands.collect { |l| [l.name, l.id]}, 0), {}, {:prompt => "select a country", :id => 'lands_select'}) %><br>
<%= f.select(:shippingservice_id, options_for_select(@shippingservices.collect { |s| [s.title.titleize, s.id] }, 0), {}, {:id => 'shippingservices_select', :prompt => "select a carrier"}) %><br>
<%= f.submit "Calculate shipping" %><br>
<% end %>

carts_controller 的显示页面显示当前的购物车。购物车模型通过 carts_items 知道它的重量。这允许我在模型中计算购物车的total_weight,并在模型中进行查询,得到available_shipping_services,我在显示页面中使用它来成功创建第一个负载,其中第一个条目显示所有可用的解决方案,但此后的每次更改都会导致第二个选择器表单无效。

{:action => "update_shipping"} 可能是错误的,稍后必须更改为另一个应该显示生成的运费的操作,但现在我专注于获取这两个选择现场工作。

再次感谢!

最佳答案

params[:land_id] 为 null,因为您尚未将其传递给 URL。您可能希望将其作为查询参数传递(GET "/carts/update_shipping/2?land_id=4"),或者更好地将其传递到 POST 请求的正文中(例如使用远程表单) )

关于mysql - AJAX 资源的复杂事件记录查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53822106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com