ruby-on-rails-3 – 活跃的管理员和金钱
发布时间:2020-12-17 02:03:39 所属栏目:百科 来源:网络整理
导读:我正在使用 money – https://github.com/RubyMoney/money gem的活跃管理员.我有一些由金钱宝石处理的属性. money gem以美分存储值.当我创建一个具有活动管理员的条目时,将在DB中创建正确的值(5000表示50.00). 但是,当我编辑一个条目时,该值乘以100,这意味着
我正在使用
money – https://github.com/RubyMoney/money gem的活跃管理员.我有一些由金钱宝石处理的属性.
money gem以美分存储值.当我创建一个具有活动管理员的条目时,将在DB中创建正确的值(5000表示50.00). 但是,当我编辑一个条目时,该值乘以100,这意味着AA显示5000表示原始输入为50.00.如果我用money属性编辑任何东西,它将乘以100.在创建时,该值通过货币逻辑,但在编辑时,不知何故活动管理员跳过该部分显示美分而不是最终货币值. 例如: form :html => { :enctype => "multipart/form-data"} do |f| f.inputs "Products" do ...... f.has_many :pricings do |p| p.input :price p.input :_destroy,:as => :boolean,:label=>"Effacer" end f.actions :publish end 型号: # encoding: utf-8 class Pricing < ActiveRecord::Base belongs_to :priceable,:polymorphic => true attr_accessible :price composed_of :price,:class_name => "Money",:mapping => [%w(price 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 convert #{value.class} to Money") } end 解决方法
Rails callbacks对于创建此类问题的解决方案非常方便.
我只想使用和after_update回调. 例: # encoding: utf-8 class Pricing < ActiveRecord::Base after_update :fix_price belongs_to :priceable,:polymorphic => true attr_accessible :price composed_of :price,"Can't convert #{value.class} to Money") } def fix_price self.price = (self.price/100) end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |