我为国家/地区选择了一个选择字段,为给定国家/地区选择了第二个选择字段,当选择了第一个国家/地区和第二个运输方式时,我想显示运输费用。

当我第一次显示显示第一个国家(仅特定重量和所选国家/地区的服务)的shippingservices资源已正确映射运输车时,但是当我对ajax的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"}可能是错误的,以后必须更改为另一个操作,该操作应显示最终的运输成本,但现在我专注于使两个选择字段起作用。

再次挂!

最佳答案

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

10-05 23:15