在编辑视图中显示时转换列的值
所以,我的主题行并没有公正地回答我的问题,但这就是我需要的:
我的产品模型中有一个价格字段。
这是我使用的表单字段,它只是一个普通的表单字段:
<%= f.label :price, "Price: Use numbers and a decimal only. E.g., 34.00" %><br />
<%= f.text_field :price, :style => "width: 50px" %>
在将其保存到数据库之前,我将这个小数转换为整数,这是因为我稍后必须用它做一些数学运算,而且整数更容易添加然后处理浮点数学。
但是,当用户再次编辑此字段时,它会在文本字段中显示为整数或 3400
。
我如何告诉 f.text_field
显示浮点值?
我的谷歌服务已关闭,因此非常感谢您的帮助。
So, my subject line isn't doing my question justice, but here is what i need:
I have a price field in my Product model.
Here is the form field I use, it's just a normal form field:
<%= f.label :price, "Price: Use numbers and a decimal only. E.g., 34.00" %><br />
<%= f.text_field :price, :style => "width: 50px" %>
I convert this decimal into an integer before i save it in the database, this is because I have to do some math later on with it, and well Integers are easier to add then dealing with float math.
However, when a user goes to Edit this field again, it comes up as an Integer or 3400
in the text field.
How do i tell f.text_field
to display the float value instead?
My google fu is off, so any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
<%= f.text_field :价格, :值 => @product.price.to_f %>
<%= f.text_field :price, :value => @product.price.to_f %>
由于这个原因,我喜欢在用户输入数据时将数据保留在数据库中。您可以添加另一列来表示要用于后面的数学运算的整数值吗?
I like to keep the data in the database as the user entered it for this reason. Could you add another column that represents the integer value to be used for the later Math?