Rare Gem and Shaper

I have a problem with forms and a money stone .

It is my problem:

  • I am creating a record that has a “amount” field (mapped to a monetary object). Let's say I enter 10 (dollars).
  • Money stone converts it to 1000 (cents).
  • I edit the same record and the form pre-populates the quantity field as 1000
  • If I save the record without changing anything, it will convert 1000 (dollars) to 100000 (cents)

How do I get to display a pre-filled dollar amount instead of cents?

Edit:

I tried editing _form.html as follows:

= f.text_field(:amount, :to_money) 

and I get this error:

 undefined method `merge' for :to_money:Symbol 
+8
ruby-on-rails currency forms rubygems formbuilder
source share
4 answers

Given the migration as follows:

 class CreateItems < ActiveRecord::Migration def self.up create_table :items do |t| t.integer :cents t.string :currency t.timestamps end end def self.down drop_table :items end end 

And the model as follows:

 class Item < ActiveRecord::Base composed_of :amount, :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 conver #{value.class} to Money") } end 

Then this form code should work fine (I just tested under Rails 3.0.3), correctly displaying and saving the dollar amount every time I save / edit. (This uses the default update / create methods for scaffolds).

 <%= form_for(@item) do |f| %> <div class="field"> <%= f.label :amount %><br /> <%= f.text_field :amount %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> 
+11
source share

If you have several fields of money in your table, and you cannot call them "cents".

 class CreateItems < ActiveRecord::Migration def self.up create_table :items do |t| t.integer :purchase_price_cents t.string :currency t.timestamps end end def self.down drop_table :items end end 

which will change your model to

 class Item < ActiveRecord::Base composed_of :purchase_price, :class_name => "Money", :mapping => [%w(purchase_price_cents cents), %w(currency currency_as_string)], :constructor => Proc.new { |purchase_price_cents, currency| Money.new(purchase_price_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 
+3
source share

Now you can edit the monetized fields (money-rails 1.3.0):

 # add migration add_column :products, :price, :price_cents # set monetize for this field inside the model class Product monetize :price_cents end # inside form use .price instead of .price_cents method f.text_field :price 

See https://stackoverflow.com>

+3
source share

Monetization and simple form, the following steps:

  1. migration

add_monetize: table ,: amount

  1. Checked Model

monetize: cent_count, allow_nil: true, numeric value: {greater_excellent: 0}

  1. Controller resolution options (do not use cent_count here)

params.require (: model) .permit (Sum)

  1. simple form input

when it is saved, it will be saved in cents in the column_cents in dB

0
source share

All Articles