在rails4

时间:2016-09-22 10:43:45

标签: javascript ruby-on-rails ruby-on-rails-4

您好我已经包含了给定文件config / countries.rb

Shipping_10_14_days = ["AD", "AF", "AL", "AM", "AS", "AW", "AZ", "BA", "BB", "BM", "BS", "BZ", "CM", "CU", "CV", "DM", "DZ", "EE", "FK", "FM", "FO", "GF", "GL", "GY", "IS", "JM", "KG", "KY", "KZ", "LB", "LC", "LI", "LT", "MQ", "MU", "MV", "NA", "NU", "PA", "PF", "PY", "RO", "SV", "TD", "UA", "UG", "UZ", "ZM"]
Shipping_4_5_days = ["AE", "BD", "BH", "IR", "JO", "JP", "KH", "KW", "LA", "LK", "MM", "MO", "NF", "OM", "PH", "PK", "QA", "SA", "IN", "VN"]
Shipping_4_7_days = ["DK", "AT", "VI", "GB", "US"]
Shipping_5_7_days = ["BA", "BE", "BG", "CH", "CR", "CY", "CZ", "DE", "EG", "ES", "FI", "FR", "GE", "GH", "GL", "GR", "HN", "HR", "HU", "IE", "IL", "IT", "LU", "LV", "MA", "MC", "MN", "MT", "NA", "NC", "NG", "NL", "NO", "PL", "PT", "SE", "SK", "TR", "TW", "TZ", "ZA", "SG"]
Shipping_3_5_days = ["AU"]
Restricted_Country = ["AR", "BN", "BR", "BS", "CL", "CN", "CO", "CR", "EC", "FK", "GP", "GT", "GU", "ID", "IS", "MP", "MX", "NZ", "PE", "PM", "RS", "RU", "TH", "TT", "UY"] 

现在我想在app / assets / javascripts / test.js中调用这些变量 请指导我如何调用这些变量

function checkShippingDays(countryValue) {
    var Shipping_10_14_days = ["AD", "AF", "AL", "AM", "AS", "AW", "AZ", "BA", "BB", "BM", "BS", "BZ", "CM", "CU", "CV", "DM", "DZ", "EE", "FK", "FM", "FO", "GF", "GL", "GY", "IS", "JM", "KG", "KY", "KZ", "LB", "LC", "LI", "LT", "MQ", "MU", "MV", "NA", "NU", "PA", "PF", "PY", "RO", "SV", "TD", "UA", "UG", "UZ", "ZM"]
    var Shipping_4_5_days = ["AE", "BD", "BH", "IR", "JO", "JP", "KH", "KW", "LA", "LK", "MM", "MO", "NF", "OM", "PH", "PK", "QA", "SA", "IN", "VN"]
    var Shipping_4_7_days = ["DK", "AT", "VI", "GB", "US"]
    var Shipping_5_7_days = ["BA", "BE", "BG", "CH", "CR", "CY", "CZ", "DE", "EG", "ES", "FI", "FR", "GE", "GH", "GL", "GR", "HN", "HR", "HU", "IE", "IL", "IT", "LU", "LV", "MA", "MC", "MN", "MT", "NA", "NC", "NG", "NL", "NO", "PL", "PT", "SE", "SK", "TR", "TW", "TZ", "ZA", "SG"]
    var Shipping_3_5_days = ["AU"]
    var Restricted_Country = ["AR", "BN", "BR", "BS", "CL", "CN", "CO", "CR", "EC", "FK", "GP", "GT", "GU", "ID", "IS", "MP", "MX", "NZ", "PE", "PM", "RS", "RU", "TH", "TT", "UY"] 

    if ($.inArray(countryValue, restrictedCountry) > -1) {
      alert('we don not ship in this country')
      $('.adress-next').prop('disabled', true);
    } else{
      $('.adress-next').prop('disabled', false);
      if ($.inArray(countryValue, shipping10to14) > -1) {
        $('.shipping-days').html('<strong>FREE</strong> Delivery (average 10-14 business days)')
      } else if ($.inArray(countryValue, shipping4to7) > -1) {
        $('.shipping-days').html('<strong>FREE</strong> Delivery (average 4-7 business days)')
      } else if ($.inArray(countryValue, shipping4to5) > -1) {
        $('.shipping-days').html('<strong>FREE</strong> Delivery (average 4-5 business days)')
      } else if ($.inArray(countryValue, shipping3to5) > -1) {
        $('.shipping-days').html('<strong>FREE</strong> Delivery (average 3-5 business days)')
      } else if ($.inArray(countryValue, shipping5to7) > -1) {
        $('.shipping-days').html('<strong>FREE</strong> Delivery (average 5-7 business days)')
      } else {
        $('.shipping-days').html('<strong>FREE</strong> Delivery (average 4-7 business days)')
      };
    };
  };

我需要在js文件中调用这些变量。

2 个答案:

答案 0 :(得分:2)

要将Rails变量传递给JS,请查看Gon Gem https://github.com/gazay/gon

它可以满足您的需求。

答案 1 :(得分:2)

创建test.js.erb文件并使用

var Shipping_10_14_days  = <%= Shipping_10_14_days %>