“数量必须是数字”Rails错误

时间:2017-06-05 17:49:00

标签: ruby-on-rails

我在我的rails应用程序上使用active_shipping gem并且读到了一个非常奇怪的错误:

amount must be a Numeric

我的送货方式正在调用错误:

def shipping
    @user = current_user
    @products = current_order.order_items.all
    @order = current_order

    packages =  []
    @products.each do |thing|
      packages << ActiveShipping::Package.new( thing.product.weight * 16,  <<<<<<<<<<ERROR CALLED ON THIS LINE
      [thing.product.box_length, thing.product.box_width, thing.product.box_depth],
      units: :imperial)
    end ## each do

    origin = ActiveShipping::Location.new( country: 'US', state: 'CO', city: 'Sedalia', zip: '80135')

    if @user.country == 'US'
      destination = ActiveShipping::Location.new( country: @user.country, state: @user.state, city: @user.city, zip: @user.zip)
    else
      destination = ActiveShipping::Location.new( country: @user.country, province: @user.state, city: @user.city, postal_code: @user.zip)
    end # if/else for country

    ups = ActiveShipping::UPS.new(login: 'lizbayardelle', password: 'UPSpassw0rd', key: '3D287D7B39D0D398')
    ups_response = ups.find_rates(origin, destination, packages)
    @ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}

    usps = ActiveShipping::USPS.new(login: '380LINCH6422')
    usps_response = usps.find_rates(origin, destination, packages)
    @usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
  end

产品重量的值位于seeds.rb文件中:

Product.delete_all
Product.create! id: 1, name: "Rule #23 Tee Shirt (Small)", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Size Small. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 2, name: "Rule #23 Tee Shirt (Medium)", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Size Medium. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 3, name: "Rule #23 Tee Shirt (Large)", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Size Large. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 4, name: "Rule #23 Tee Shirt (XL)", weight: 1, box_width: 12, box_length: 14, box_depth: 3, price: 32.00, short_description: "Size XL. Spread the word. These tee shirts not only have the Manly Art crest, but they also have a man rule on the back to remind any passers-by that may have forgotten.", active: true
Product.create! id: 5, name: "Grill Glove", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Protect your manly mitts from the fiery dangers of the grill.", active: true
Product.create! id: 6, name: "BBQ Apron", weight: 1, box_width: 6, box_length: 9, box_depth: 2, price: 32.00, short_description: "Standard grilling apron with three pockets for all your necessary tools.  You get to add the grease marks and stains from the marinade of your choosing.", active: true

谁能看到这里发生了什么?谷歌/ Stack Overflow似乎没有听说过这个错误,这令人担忧......

1 个答案:

答案 0 :(得分:0)

我最终为每件事添加了.to_i,并且由于某种原因使它工作。