表单未保存到 Rails 数据库中
我目前有一个表单:
<%= f.label(:price) %> <br/>
<%= f.text_field(:price, :value => number_to_currency(@object.price)) %>
我将迁移从使用 float 更改为使用decimal:
change_column :object, :price, :decimal, :precision => 5, :scale => 2
在我看来,我使用以下方式调用它:
<%= @object.price %>
出于某种原因,每当我对表单或在控制台中进行更改时,它都不会保存该值并保留不管我把它改成什么,它的价格都是 0.00 美元。在视图中,它始终显示为“0.0”。我不确定问题是什么。
I currently have a form:
<%= f.label(:price) %> <br/>
<%= f.text_field(:price, :value => number_to_currency(@object.price)) %>
I changed my migration from using float to using decimal:
change_column :object, :price, :decimal, :precision => 5, :scale => 2
On my view, I called it using:
<%= @object.price %>
For some reason, whenever I make a change to the form or in the console, it never saves the value and keeps it at $0.00 regardless of what i change it to. In the view, it always shows up as '0.0'. I am not sure what the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有一个名为
object
或objects
的表,或者这只是一个示例?如果没有,请检查您的价格栏以确保迁移正常进行。
如果您的表中确实有正确的列类型,请检查您是否在
Object
类中使用attr_accessible
(以及price
包括)。另外,如果您使用
Object
作为类名,您可能会遇到其他一些问题,我建议您不要这样做。编辑
number_to_currency
可能会在您的费用前面加上 $,请确保您输入的价格前没有任何货币,否则我相信这也会导致 0.0(无法解析十进制“$123.00”) ,但可以解析'123.00'Do you have a table named
object
orobjects
or is this just an example?If not, check your price column to make sure the migration worked properly.
If you do have a proper column type in your table, check to see if you're using
attr_accessible
in theObject
class (and thatprice
is included).Also, if you are using
Object
as your class name, you may have some other issues here and I'd advise against it.EDIT
number_to_currency
may prepend a $ in front of your cost, make sure you're entering your price without any currency before it or else I believe this would also result in 0.0 (can't parse decimal '$123.00', but can parse '123.00'