【问题标题】:composed_of & fields_for组成的&fields_for
【发布时间】:2010-07-09 01:00:30
【问题描述】:

所以我已经阅读并重新阅读了关于该主题的 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' 的属性。我真的被困住了:/

【问题讨论】:

    标签: ruby-on-rails fields-for


    【解决方案1】:

    编辑:我之前误解了你的问题。是的,在您的表单中,您需要通过这种方式引用 amount

    - form_for @payment do |p|
       = a.label :dollars
       = a.text_field :amount
       = a.label :currency
       = a.text_field :currency, :value => :usd
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      相关资源
      最近更新 更多