I use a money stone and I wanted to know how I can get prices such as 23.45, 10.00, 0.32 to show in one field like a dollar .cents? I want the field of my cents to become just the price (money or dollars and cents).
Here is my code:
My money gem model is composed:
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
Shape (I cut parts for short):
<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>
My migration or pricing table:
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
It seems like I'm missing a column or something like that. Maybe it's even cents? Not sure, so I need your help.
Thanks for taking the time.