我为国家/地区选择了一个选择字段,为给定国家/地区选择了第二个选择字段,当选择了第一个国家/地区和第二个运输方式时,我想显示运输费用。
当我第一次加载运送购物车时,显示为第一个国家/地区正确映射了shippingservices资源(仅针对特定重量和所选国家/地区的服务),但是当我对update_shipping操作使用相同的查询时我得到postzones.land_id = NULL
我的控制器:
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部分归功于@rewrite:
$(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)
我想一切都设置正确,但是由于控制器正在寻找(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模型通过carts_items知道其重量。这使我可以在模型中计算购物车的total_weight并在模型中进行查询,从而得出available_shipping_services,我在显示页面中使用该服务成功创建了第一个负载,其中第一个条目显示了所有可用的解决方案,但此后的每次更改都会导致第二个选择器表单无效。
{:action => "update_shipping"}
可能是错误的,以后必须更改为另一个操作,该操作应该显示最终的运输成本,但是现在我专注于使两个选择字段起作用。
再次挂断!
答案 0 :(得分:1)
params[:land_id]
为空,因为您尚未将其传递到URL。您可能希望将其作为查询参数(GET "/carts/update_shipping/2?land_id=4"
)传递,甚至最好在POST请求的正文中传递(例如,使用远程表单)