所以我读过&重新读过关于这个话题的文章(http://www.stephenchu.com/2008/05/rails-composedof-validation.html),但是我没有任何运气。
根据money 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我收到的错误是:未定义的方法‘美分’用于{“美分”=>“6.66”,“货币”“=>”美元}:HashWithIndifferentAccess
‘composed_of’散列以‘美分’的形式传递到中,但根据本文的说法,它应该使用表单中的params中的'amount‘属性。我真的被困住了
发布于 2011-01-31 22:03:01
编辑:我以前误解了你的问题。是的,在您的表单中,您需要这样引用amount:
- form_for @payment do |p|
= a.label :dollars
= a.text_field :amount
= a.label :currency
= a.text_field :currency, :value => :usdhttps://stackoverflow.com/questions/3209146
复制相似问题