【问题标题】:Setting currency with form select with Money gem使用 Money gem 使用表单选择设置货币
【发布时间】:2011-11-03 00:08:14
【问题描述】:

我已经在这个上工作了好几个小时,但我无法弄清楚这个。

我愿意让用户为他在表单中填写的价格选择相应的货币。

我正在使用 Money Gem (https://github.com/RubyMoney/money)。所有值都设置正确,但不是仅设置为其默认值 (USD) 的货币,无论通过表单发送。我想这是我对金钱宝石缺乏经验。

在我的模型中:

 require 'money'
  composed_of :price_per_unit,
    :class_name => "Money",
    :mapping => [%w(cents_per_unit cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents_per_unit, currency| Money.new(cents_per_unit || 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") }

  composed_of :total_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") }

:price_per_unit 和 :total_price 共享 :currency 属性。

:price_per_unit 使用 cents_per_unit 和货币 :total_price 使用美分和货币

在我的控制器中:

  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 << [name, iso_code]
     end
  end

在我的新表单视图中:

      <p>
        <%= f.select(:currency, @currencies) %>
      </p>
      <p>
        <%= f.label :price_per_unit %>
        <%= f.number_field :price_per_unit, :size => 5, :value => 0, :step => 0.01, :min => 0 %> 
      </p>
      <p>
        <%= f.label :total_price %>
        <%= f.number_field :total_price, { :step => 1, :size => 10, :value => 0 } %> 
      </p>

我确实尝试将默认货币更改为澳元,然后无论我在选择中选择什么,我的所有记录都会设置为澳元。

在我的日志中:

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"=>"✓", "feedstock"=>"", "power_plant_substrate"=>{"power_plant_id"=>"59", "substrate_id"=>"159", "quantity"=>"1", "trade"=>"selling", "currency"=>"JPY", "price_per_unit"=>"1.00", "total_price"=>"1", "address"=>"Pélussin, France", "transport"=>"pickup_only", "latitude"=>"", "longitude"=>"", "description"="sadf"}, "commit"=>"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)

有什么想法吗?

干杯,

乔尔

【问题讨论】:

  • 在create方法中加一行解决。用解决方案编辑原始问题

标签: ruby-on-rails forms currency


【解决方案1】:

解决了!!

在create方法中加一行解决:

def create
    @power_plant_substrate = PowerPlantSubstrate.new(params[:power_plant_substrate])

    # here I am setting the currency with what was sent in params
    @power_plant_substrate.currency = params[:power_plant_substrate][:currency] <=== this line

...

end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多