首页 > 解决方案 > 编辑时Rails价格修改

问题描述

我有一个edit显示价格的表格:

<%= form.text_field :price, value: number_to_currency(purchase.price), class: "form-control" %>

在此处输入图像描述

当我更新控制器中的表单时,我收到:

#<ActionController::Parameters {"wine_item_id"=>"12", "quantity"=>"3",
"price"=>"$12,122.00", "store_attributes"=>#<ActionController::Parameters
{"name"=>"Cuvee 3000"} permitted: true>} permitted: true>

但更新后价格变为:

在此处输入图像描述

提前致谢

标签: ruby-on-railsruby

解决方案


如果您键入以下内容,控制台会返回什么:

WineItem.find(12).price

是“$12,122.00”吗?

因为在我看来您正在尝试在字符串上运行 to_f,这将导致 0.0

你可以在irb中自己查看:

"abc".to_f 将等于 0.0


推荐阅读