1

そのため、このトピックに関するStephen Chuの記事( http://www.stephenchu.com/2008/05/rails-composedof-validation.html )を読んで読み直しましたが、運が悪かっただけです。

マネージェムのドキュメント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'としてcomposed_ofに渡されますが、記事によると、フォームのparamsで'amount'の属性を使用する必要があります。私は本当に立ち往生しています:/

4

1 に答える 1

0

編集:以前あなたの質問を誤解しました。はい、フォームでは次のように参照する必要がありますamount

- form_for @payment do |p|
   = a.label :dollars
   = a.text_field :amount
   = a.label :currency
   = a.text_field :currency, :value => :usd
于 2011-01-31T22:03:01.160 に答える