我有一个国家的选择字段,一个给定国家的航运选项第二个选择字段,当第一个国家和第二次航运已被选中,我想显示航运成本。
当我第一次用show加载shippingservices时,shippingservices资源被正确地Map到第一个国家(只有特定权重和所选国家的服务),但是当我对ajax资源的update\u 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部分得益于@rewrited:
$(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/\u 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\u id=parameters:{“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\u控制器的“显示”页面显示当前装运车。carts模型通过carts\u项来知道它的重量。这允许我在模型中计算购物车的总重量,并在模型中进行查询,从而生成可用的装运服务,我在显示页面中使用该服务成功地创建了第一个加载,其中第一个条目显示了所有可用的解决方案,但之后的每一个更改都会导致第二个选择器窗体无效。
这个 {:action => "update_shipping"}
可能是错误的,以后必须更改为另一个操作,该操作应显示生成的运输成本,但目前我正在集中精力使两个select字段正常工作。
又是唐卡!
1条答案
按热度按时间brc7rcf01#
params[:land_id]
为空,因为您尚未将其传递到url。您可能希望将其作为查询参数传递(GET "/carts/update_shipping/2?land_id=4"
)或者最好在post请求的主体中传递它(例如使用远程窗体)