简体   繁体   English

当我输入值“45”时出现错误,我在轨道上得到“0.45e2”ruby

[英]bug when i input value “45” i get “0.45e2” ruby on rails

i cant understand why when i input 45 i get 0.45e2 while any sting like pants work fine i have the following html我不明白为什么当我输入 45 时我得到 0.45e2 而任何像裤子这样的刺都可以正常工作我有以下 html

<%= form_with(model: invoice, local: true) do |form| %>
  <% if invoice.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
      <ul>
        <% invoice.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
<center><h1>ΤΔΑ-ΤΙΜΟΛΟΓΙΟ-ΔΕΛΤΙΟΥ ΑΠΟΣΤΟΛΗΣ</h1></center>
<div align="right">

</div>

<pre>ID           CODE         DESCRIPTION  BASE CODE    QUANTITY     </pre>
<table style="border:2px solid black;"id="tableid">
<td><input  type="text" name="invoice[items][]" /></td>
<td><input  type="number" name="invoice[tax][]" /></td>
<td><input  type="number" name="invoice[discount][]" /></td>
<td><input  type="number" name="invoice[sum][]" /></td>
<td><input  type="number" name="invoice[price][]" /></td>
</tr>
</table>
<script type="text/javascript">
    var table = document.getElementById("tableid");
    table.addEventListener("keyup", function(event) 
    {
    if (event.keyCode === 18)
     {
            event.preventDefault();
            var rowid=table.rows.length-1;
            var row = table.insertRow(rowid+1);
            row.id = (rowid+1).toString();
            var cell0 = row.insertCell(0);
            var cell1 = row.insertCell(1);
            var cell2 = row.insertCell(2);
            var cell3 = row.insertCell(3);
            var cell4 = row.insertCell(4);
            cell0.innerHTML = "<input type='text'  size='8' name='invoice[items][]'></input>";
        cell1.innerHTML = "<input type='text'  size='8' name='invoice[tax][]'></input>";
        cell2.innerHTML = "<input type='text'  size='8' name='invoice[discount][]'></input>";
        cell3.innerHTML = "<input type='text'  size='8' name='invoice[sum][]'></input>";
        cell4.innerHTML = "<input type='text'  size='8' name='invoice[price][]'></input>";

      }
    });
</script>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

and my controller和我的 controller

    def invoice_params
      params.require(:invoice).permit({:items => []}, {:price => []}, {:tax => []}, {:discount => []}, {:sum => []})
    end

so this is happening when i create new items所以当我创建新项目时会发生这种情况

Invoice was successfully created.

Items: ["pants", "spoon"]

Price: [0.45e2, 0.2e1]

Tax: [0.45e2, 0.25e2]

Discount: [0.45e2, 0.2e2]

Sum: [0.45e2, 0.22e2]

Edit | Back

should i tell ruby on rails tat the params sum,tax,discoun is numeric or something like that?我应该告诉 ruby on rails tat params sum,tax,discoun 是数字还是类似的东西? or must be a better way to save the params?还是必须是保存参数的更好方法? as the string work fine因为字符串工作正常

UPDATE also if you change t.numeric to t.float solve the problem如果您将 t.numeric 更改为t.float也可以更新以解决问题

It's best to avoid floating point numbers completely when dealing with money and convert to eg cents (or whatever your currency's smallest denomination is called) for storage.处理货币时最好完全避免浮点数,并转换为例如美分(或任何您的货币的最小面额)进行存储。 If you know you'll only ever need to support one currency this will be simple.如果您知道您只需要支持一种货币,这将很简单。 Of course, you still want to allow for entering and displaying prices via decimal notation.当然,您仍然希望允许通过十进制表示法输入和显示价格。 Here's how I would approach that in your Invoice model, after creating new price_cents etc. columns and migrating the existing data:在创建新的price_cents等列并迁移现有数据之后,我将在您的Invoice model 中处理该问题:

  def price=(value)
    value = value.to_f if value.is_a? String
    self.price_cents = (value * 100).round
  end

  def price
    price_cents&.positive? ? price_cents / 100.0 : 0
  end

暂无
暂无

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

相关问题 如何在活动管理员中获取 form.input 的值(ruby on rails) - How do I get the value of a form.input in active admin (ruby on rails) 重建宝石时,created_at的值错误。 Ruby on Rails - Created_at get a wrong value when I rebuild my gems . Ruby on Rails 当我在导轨上安装 ruby 后检查导轨版本时,我收到以下消息: - When I check the version of rails after installing ruby on rails I get this message: Ruby on Rails - 将方法的返回值传递给has_attached_file。 我的Ruby语法错了吗? - Ruby on Rails - passing a returned value of a method to has_attached_file. Do I get Ruby syntax wrong? 当我在 Rails 上的 Ruby 中编写功能测试时,我收到对 GET/POST 请求的 403 响应 - When I write a functional test in Ruby on Rails, I get a 403 response to a GET/POST request 当我尝试从 ruby​​ on rails 上的 Form 获取输入时,我得到了简单名称的哈希数组瞬间。 如何解决? - When I tried to get input from the Form on ruby on rails, I am getting back hash array instant of simple name. How to solve it? 如何在Ruby / Rails中将“ 2014/11/23 8:35 PM”转换为“ 2014-11-16 07:45:00”? - How can I convert “11/23/2014 8:35 PM” to “2014-11-16 07:45:00” in Ruby/Rails? 耙耙时出现段错误(Ruby on Rails) - Segfault when I rake (Ruby on Rails) 使用mongrel_rails在rails app上启动ruby时,为什么会出现错误 - Why do I get an error when starting ruby on rails app with mongrel_rails 我可以获取Ruby on Rails组件列表吗? - Can i get Ruby on Rails components List?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM