新手问题 为什么新增还是编辑商品价格都始终显示为 0.0

wangyzyoga · January 21, 2015 · Last by wangyzyoga replied at January 21, 2015 · 2057 hits

练习《web 开发敏捷之道 3nd-rails 进行 web 开发》中对应程序,在第 6 章节维护商品时,增加 price 字段,但是不管新增还是编辑商品,保存后商品价格始终显示为 0.0。不知道哪里出问题? 开发环境:Rails 4.2.0,Ruby 2.1.2。部分代码如下:

t.decimal  "price",       precision: 8, scale: 2, default: 0.0

index 中增加:

<td><%= product.price %></td>

form 中增加:

<div class="field">
        <%= f.label :price %><br>
        <%= f.text_field :price %>
</div>

show 中增加:

<p>
  <strong>Price:</strong>
  <%= @product.price %>
</p>

controller 的 strong params permit 有没有添加 price ? 这个在日志里应该有提示的

#1 楼 @foxzool 哦,我在日志看到这个错误提示,但是具体怎么加?

你用的是 rails4 版本,就不要看你现在这本书啦,这本是基于 rails3 的,更换最新的。rail3 跟 rails4 多少会有些不同,如果你要学这本书,那就按照书中的版本来练习吧,不然你可能会有更多的问题出现的!

#1 楼 @foxzool 问题已解决,谢谢! #3 楼 @kai209209 先按照这书学,遇到问题慢慢解决。

You need to Sign in before reply, if you don't have an account, please Sign up first.