Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 由&;字段_Ruby On Rails_Fields For - Fatal编程技术网

Ruby on rails 由&;字段

Ruby on rails 由&;字段,ruby-on-rails,fields-for,Ruby On Rails,Fields For,因此,我已经阅读并重读了朱棣文(Stephen Chu)关于这个主题的文章(),但是我没有任何运气 根据money gem文档money.rubyforge.org中提供的示例 型号 class Payment < ActiveRecord::Base composed_of :amount, :class_name => "Money", :mapping => [%w(cents cents), %w(currency currency_as_strin

因此,我已经阅读并重读了朱棣文(Stephen Chu)关于这个主题的文章(),但是我没有任何运气

根据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
我收到的错误是: {“美分”=>“6.66”,“货币”=>“美元”}的未定义方法“美分”:hashWithInferenceAccess


“amount”散列被作为“cents”传递到composited_中,但根据文章,它应该在表单的params中使用“amount”属性。编辑:我以前误解了你的问题。是的,在您的表格中,您需要通过以下方式引用
金额

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