【问题标题】:Price input in Rails4Rails4 中的价格输入
【发布时间】:2013-07-29 06:16:34
【问题描述】:

我最近切换到 Rails4 并且我是。不是。得到。事物。到。工作...

ANSWER: As Aman remembered me, in Rails 4 we have to filter attributes in the Controller.

我想将定价添加到我的列表脚手架。

1.) 生成迁移

rails g migration AddPriceToListings price:decimal

2.) 编辑迁移:

add_column :listings, :price, :decimal, :precision => 8, :scale => 2

3.) 向我的表单添加输入

<!-- Price Field -->
<div class="control-group">
  <label class="control-label">Price</label>
  <div class="controls">
    <%= f.input :price, :placeholder => "0.00", label: false %>
  </div>
</div>

4.) 在展示页面上

<%= number_to_currency(@listing.price, :unit => "$") %>

但在我的 Shopage 上什么都没有。价格不会显示。它甚至没有出现在数据库中。

有什么建议吗?

【问题讨论】:

  • 我试过这个看起来不错。
  • 那么你不在 Rails 4 上,正如 Aman 回答的(我忘了)你必须在列表控制器中过滤你的属性。 :)
  • 可惜我还没有切换到 Rails 4。;)

标签: ruby-on-rails migration simple-form ruby-on-rails-4


【解决方案1】:

在 Rails4 中,如果您通过表单进行批量分配,则必须允许参数:

确保在 listings_controller.rb 中过滤属性:

def create
  @listing = Listing.create(listing_params)
  ...
end
...
private
  def listing_params
    params.require(:listing).permit(:price, :name) #name can be replaced by other parameters received via form.
  end

【讨论】:

  • 我在字面上思考和过度思考了半个小时,完全忘记了新的属性过滤:) 非常感谢阿曼!
【解决方案2】:

我认为你错过了迁移文件中的:decimal

通过这个再次编辑迁移

add_column :listings, :price, :decimal, :precision => 8, :scale => 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多