So I have read & re-read the Stephen Chu article on the topic (http://www.stephenchu.com/2008/05/rails-composedof-validation.html), however I am just not having any luck.
Per the example on provided in the money gem documentation money.rubyforge .org
Model
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
Nice skinny controller
class PaymentsController < ApplicationController
def create
@payment = Payment.new(params[:payment])
...
end
end
View (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
The error I receive is: undefined method `cents' for {"cents"=>"6.66", "currency"=>"usd"}:HashWithIndifferentAccess
The 'amount' hash is being passed into composed_of as 'cents', but according to the article, it should use the attributes of 'amount' in params from the form. I'm just really stuck :/