我在我的 Rails 应用程序中使用money-rails
gem。我有模型Transaction
。
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 个额外的零。
而且我真的不知道如何解决它。也许有人以前遇到过这样的问题?