所以我已经阅读并重新阅读了关于该主题的 Stephen Chu 文章(http://www.stephenchu.com/2008/05/rails-composedof-validation.html),但是我没有任何运气。
根据货币 gem 文档 money.rubyforge .org 中提供的示例
模型
class Payment < 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) }
end
漂亮的瘦控制器
class PaymentsController < ApplicationController
def create
@payment = Payment.new(params[:payment])
...
end
end
查看 (HAML)
- form_for @payment do |p|
- p.fields_for :amount, @payment.amount do |a|
= a.label :cents
= a.text_field :cents
= a.label :currency
= a.text_field :currency, :value => :usd
我收到的错误是:{"cents"=>"6.66", "currency"=>"usd"}:HashWithIndifferentAccess 的未定义方法`cents'
'amount' 散列被作为'cents' 传递给composer_of,但根据文章,它应该在表单的参数中使用'amount' 的属性。我真的被困住了:/