ruby-on-rails - 编辑时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>
但更新后价格变为:
提前致谢
解决方案
如果您键入以下内容,控制台会返回什么:
WineItem.find(12).price
是“$12,122.00”吗?
因为在我看来您正在尝试在字符串上运行 to_f,这将导致 0.0
你可以在irb中自己查看:
"abc".to_f 将等于 0.0
推荐阅读
- typescript - Symfony/Webpack.encore 配置,添加“模块规则”以便在 vue.js 中使用 typescript
- java - 为什么 CONTENT_DISPOSITION 标头被认为是不安全的?
- git - Vscode:如何更改保存的 github 用户名和密码?
- unix - 文件夹结构中某些文件的“zip 名称不匹配”
- reactjs - 如何从表单反应更新堆栈数组
- r - 如何将公式传递给 lm() 而不会出错?
- java - Java getStacktrace 返回错误的行号
- c++ - 在 C++17 中以毫秒为单位获取时间?
- python - 关闭/关闭 Python Websocket
- php - 为什么 phpseclibsftp 返回“目录 '%s' 不存在并且无法创建 (%s)。”?