Dav*_*vid 9 forms rubygems currency ruby-on-rails formbuilder
我遇到了表格和金钱宝石的问题.
这是我的问题:
如何让它以美元而非美分显示预先填充的金额?
编辑:
我尝试像这样编辑_form.html:
= f.text_field(:amount, :to_money)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
undefined method `merge' for :to_money:Symbol
Run Code Online (Sandbox Code Playgroud)
Dyl*_*kow 11
鉴于迁移如下:
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.integer :cents
t.string :currency
t.timestamps
end
end
def self.down
drop_table :items
end
end
Run Code Online (Sandbox Code Playgroud)
一个模型如下:
class Item < 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) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't conver #{value.class} to Money") }
end
Run Code Online (Sandbox Code Playgroud)
然后这个表单代码应该完美地工作(我刚刚在Rails 3.0.3下测试),每次保存/编辑时正确显示和保存美元金额.(这是使用默认的脚手架更新/创建方法).
<%= form_for(@item) do |f| %>
<div class="field">
<%= f.label :amount %><br />
<%= f.text_field :amount %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)