简体   繁体   English

Rails数量奇异地添加(将运费总计)

[英]Rails Numbers Adding Weirdly (Adding Shipping Rate to Total)

I have a store set up on my app and in one stage of the checkout process it has to add the shipping rate to the subtotal to get the total (should be simple). 我在我的应用上设置了一家商店,在结帐流程的一个阶段中,它必须将运费添加到小计中才能得出总额(应该很简单)。 However, when the shipping rate is a single digit (eg ($2.00)) it adds correctly like this: 但是,当运费为一位数(例如($ 2.00))时,它会像这样正确添加:

$32 subtotal + $6 shipping = $38 total 小计$ 32 +运费$ 6 =总计$ 38

HOWEVER, when the shipping rate is a double digit it adds like this: 但是,当运费是两位数时,它会像这样添加:

$32 subtotal + $23 shipping = $34 total 小计$ 32 +运费$ 23 =总计$ 34

From what I can tell it must be something hinky with the place values, but I can't figure out what it is, leading me to believe there's some idiosyncratic Ruby thing I'm doing wrong. 据我所知,这肯定与位值有关,但我不知道它是什么,这使我相信我在做某些特质的Ruby错误。

On my ERB view, here is how the form looks: 在我的ERB视图上,表单如下所示:

  <% shipping_choices = [] %>
    <% @usps_rates.each do |rate| %>
      <% choice = [] %>
      <% choice << [rate[1], rate[0]] %> <!-- value ( $, desc ) -->
      <% choice << number_to_currency(rate[1]/100, precision: 2).to_s + " - " + rate[0].to_s %> <!-- description -->
      <% shipping_choices << choice %>
    <% end %>
    <%= simple_form_for @order, url: charges_update_order_path(:shipping), method: :post do |f| %>
      <div class="row">
        <div class="form-inputs text-left">
          <div class="form-group">
            <%= f.collection_radio_buttons :shipping, shipping_choices, :first, :last, item_wrapper_class: :block_radio_button_collection %>
          </div>
        </div> <!-- form inputs -->
      </div> <!-- choices row -->
      <div class="row">
        <%= f.button :submit, "Continue to Billing", class: "btn btn-manly" %>
      </div>
    <% end %>

And this is the order model where the information is being saved: 这是保存信息的order模型:

def update_order_from_shipping_page(shipping_pair)
  self.shipping = shipping_pair[0]
  self.shipping_choice = shipping_pair[1]
  new_total = self.subtotal + self.shipping
  self.update_attributes(total: new_total, shipping_choice: self.shipping_choice)
end

Can anyone see where things are going wrong? 谁能看到哪里出了问题?

It turned out that this line: 原来这行:

number_to_currency(rate[1]/100, precision: 2).to_s

Needed parentheses around it like this: 所需的括号如下所示:

(number_to_currency(rate[1]/100, precision: 2)).to_s

For some reason that did the trick. 由于某种原因,成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM