Я использую денежный камень , и я хотел бы узнать , как я могу получить цены, такие как 23,45, 10,00, 0,32, для отображения в одном поле как dollar.cents? Я хочу, чтобы моиполе центов, чтобы стать только Цена (деньги или доллары и центы).
Вот мой код:
Моя модель с жемчужиной денег состояла из:
class Price < ActiveRecord::Base
attr_accessible :name, :date, :cents, :currency
belongs_to :user
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :currency, :presence => true
validates :cents, :presence => true
validates :date, :presence => true
default_scope :order => 'prices.created_at DESC'
composed_of :price,
:class_name => "Money",
:mapping => [%w(cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
end
Форма (я вырезал части длякраткость):
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :cents %><br />
<%= f.text_field :cents %>
</div>
<><div class="field">
<%= f.label :currency %><br />
<%= f.select(:currency,major_currencies(Money::Currency::TABLE),
{:include_blank => 'Select a Currency'}) %>
</div>
Моя таблица миграции или цен:
class CreatePrices < ActiveRecord::Migration
def self.up
create_table :prices do |t|
t.integer :user_id
t.string :name
t.date :date
t.integer :cents, :default => 0
t.string :currency
Похоже, мне не хватает столбца или чего-то еще.Предполагается, что даже одни центы?Не уверен, поэтому мне нужна ваша помощь.
Спасибо, что нашли время.