Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting currency with form select with Money gem
    primarykey
    data
    text
    <p>I have been working quite a few hours on this one but I can't figure out this one. </p> <p>I am willing to have the user select a corresponding currency for the price he is filling in the form. </p> <p>I am using the Money Gem (https://github.com/RubyMoney/money ). All values are set correctly BUT not the currency that only sets to its default value (USD) whatever is sent through the form. I suppose this is my lack of experience with the Money gem. </p> <p>In My Model:</p> <pre><code> require 'money' composed_of :price_per_unit, :class_name =&gt; "Money", :mapping =&gt; [%w(cents_per_unit cents), %w(currency currency_as_string)], :constructor =&gt; Proc.new { |cents_per_unit, currency| Money.new(cents_per_unit || 0, currency || Money.default_currency) }, :converter =&gt; Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") } composed_of :total_price, :class_name =&gt; "Money", :mapping =&gt; [%w(cents cents), %w(currency currency_as_string)], :constructor =&gt; Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }, :converter =&gt; Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") } </code></pre> <p>:price_per_unit and :total_price share in common the :currency attribute.</p> <p>:price_per_unit working with cents_per_unit and currency :total_price working with cents and currency</p> <p>In my controller:</p> <pre><code> def new @power_plant_substrate = PowerPlantSubstrate.new # preparing the form select for the currencies @currencies = [] major_currencies(Money::Currency::TABLE).each do |currency| name = Money::Currency::TABLE[currency][:name] iso_code = Money::Currency::TABLE[currency][:iso_code] @currencies &lt;&lt; [name, iso_code] end end </code></pre> <p>In my new form view:</p> <pre><code> &lt;p&gt; &lt;%= f.select(:currency, @currencies) %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :price_per_unit %&gt; &lt;%= f.number_field :price_per_unit, :size =&gt; 5, :value =&gt; 0, :step =&gt; 0.01, :min =&gt; 0 %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :total_price %&gt; &lt;%= f.number_field :total_price, { :step =&gt; 1, :size =&gt; 10, :value =&gt; 0 } %&gt; &lt;/p&gt; </code></pre> <p>I did try changing the default currency to AUD, then all my records get set to AUD whatever my pick in my select.</p> <p>In my log:</p> <pre><code>Started POST "/power_plant_substrates" for 127.0.0.1 at 2011-11-03 00:40:38 +0100 Processing by PowerPlantSubstratesController#create as JS Parameters: {"utf8"=&gt;"✓", "feedstock"=&gt;"", "power_plant_substrate"=&gt;{"power_plant_id"=&gt;"59", "substrate_id"=&gt;"159", "quantity"=&gt;"1", "trade"=&gt;"selling", "currency"=&gt;"JPY", "price_per_unit"=&gt;"1.00", "total_price"=&gt;"1", "address"=&gt;"Pélussin, France", "transport"=&gt;"pickup_only", "latitude"=&gt;"", "longitude"=&gt;"", "description"="sadf"}, "commit"=&gt;"Save"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 21]] PowerPlant Load (0.3ms) SELECT "power_plants".* FROM "power_plants" WHERE "power_plants"."user_id" = 21 AND "power_plants"."name" = 'My Tradings' LIMIT 1 SQL (0.7ms) INSERT INTO "power_plant_substrates" ("address", "cents", "cents_per_unit", "created_at", "currency", "description", "gmaps", "latitude", "locale", "longitude", "period","power_plant_id", "quantity", "state", "substrate_id", "trade", "transport","unit_of_measure", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?) [["address", "Pélussin, France"], ["cents", 100], ["cents_per_unit", 100], ["created_at", Wed, 02 Nov 2011 23:40:39 UTC +00:00], ["currency", "CAD"], ["description", "sadf"], ["gmaps", true], ["latitude", 45.417608], ["locale", "us"], ["longitude", 4.676041], ["period", "year"], ["power_plant_id", 59], ["quantity", 1], ["state", "open"], ["substrate_id", 159], ["trade", "selling"], ["transport", "pickup_only"], ["unit_of_measure", "mass"], ["updated_at", Wed, 02 Nov 2011 23:40:39 UTC +00:00]] Completed 200 OK in 1107ms (Views: 4.3ms | ActiveRecord: 2.5ms) </code></pre> <p>Any ideas?</p> <p>Cheers, </p> <p>Joël</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload