1
我在我的rails應用程序中使用money-rails
寶石。我有型號Transaction
。2額外的零錢貨幣欄杆
class Transaction < ActiveRecord::Base
monetize :amount_money, as: :amount, with_model_currency: :currency, numericality: {greater_than_or_equal_to: 0}
end
用於添加新事務的表單。
= simple_form_for [:post_manager, @transaction] do |f|
= f.label t('activerecord.attributes.transaction.amount')
= f.input :amount, class: 'form-control'
= f.submit t('views.transactions.submit.create'), class: 'btn btn-md btn-success'
在我的控制器動作:
def create
@transaction = Transaction.new(transaction_params)
@transaction.save
respond_with(@transaction, location: post_manager_transactions_path)
end
在我的錢初始化:
MoneyRails.configure do |config|
config.register_currency = {
priority: 1,
iso_code: 'BYR',
name: 'Belarusian Ruble',
symbol: 'Br',
disambiguate_symbol: 'BYR',
subunit_to_unit: 1,
symbol_first: false,
decimal_mark: '.',
thousands_separator: ' ',
iso_numeric: '974',
smallest_denomination: 100
}
end
當我嘗試添加新的交易:
在我的控制器動作:
[1] pry(#<PostManager::TransactionsController>)> @transaction
=> #<Transaction:0x000001018ebfa0 id: nil, kind: "withdraw", amount_money: 12300, note: "vf", approved: nil, wallet_id: 1, category_id: 1, created_at: nil, updated_at: nil>
[2] pry(#<PostManager::TransactionsController>)> params
=> {"utf8"=>"✓",
"authenticity_token"=>"hAHFdamHK7CI41zXiHUCSb+RUg+57JR9sZTIhi2frcLEQELakQuOvhs8xaWMwK32XbxTsTfplCQJA7XigsueLQ==",
"transaction"=>{"kind"=>"withdraw", "category_id"=>"1", "amount"=>"123", "note"=>"vf"},
"commit"=>"Создать операцию",
"controller"=>"post_manager/transactions",
"action"=>"create"}
所以。在我的參數中:金額是123,但是在我的新交易中:金額是12300,所以我在金額貨幣字段中有2個額外的零。
我真的不知道如何解決它。也許有人以前有過這樣的問題?
我相信這兩個零是kopejki =) – 2014-11-25 12:16:10
@МалъСкрылевъ,不,這不是kopejki :) – 2014-11-25 12:21:03
錢寶石做它的計算中美分。他們聲稱這個策略的問題較少。 Такчтоэтовсетакикопейки:)'表示貨幣值爲整數,單位爲美分。這可以避免浮點舍入錯誤 – 2014-11-25 14:20:06