[英]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.