我有型号产品,字段price_cents和price_currency.货币默认货币是美元.
模型:
class Product < ActiveRecord::Base CURRENCIES = %w(USD EUR) composed_of :price,:class_name => "Money",:mapping => [%w(price_cents cents),%w(price_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
形成:
= form_for @product do |f| = f.label :price = f.text_field :price = f.select :price_currency,Product::CURRENCIES = f.submit
控制器创建方法:@product = Product.new(params [:product])
问题:当用户将price字段设置为100并将price_currency设置为EUR时,它会使用默认货币(USD)创建价格.我该怎么办呢?是可以在视图中执行它还是我应该在控制器中执行它(例如@ product.price.currency = …)?