【发布时间】:2011-09-08 20:30:47
【问题描述】:
我的用户可以输入和创建价格。我想知道如何做到这一点,所以当用户输入 23.5 他们得到 23.50 而不是 0.0 他们得到 0.00。如何将以下能力添加到 t.decimal 或 my price:decimal?
感谢您的帮助!
答案(:价格与规模和精度)
class CreatePrices < ActiveRecord::Migration
def self.up
create_table :prices do |t|
t.string :price_name
t.decimal :price, :precision => 10, :scale => 2
t.date :date
t.timestamps
end
end
def self.down
drop_table :prices
end
end
Schema.rb:
create_table "prices", :force => true do |t|
t.string "price_name"
t.decimal "price", :precision => 10, :scale => 2
t.date "date"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
我的脚手架形式:
然后在你看来把 number_to_currency(@model.attribute):
价格:
【问题讨论】:
-
似乎是输出问题。使用
number_to_currency(price)显示输出。显示显示价格的代码部分。
标签: ruby-on-rails