我的模特儿里有这个:
monetize :advance_amount_cents, allow_nil: true
monetize :rental_amount_cents, allow_nil: true我使用AutoNumeric来显示货币。它把它发送回控制器,像这样,在params中:
'rental_amount' = "2050.12"它从模型中返回此错误:
activerecord.errors.models.rental_period.attributes.rental_amount.invalid_currency它接受货币时,我可以让它用逗号而不是小数点发送。这里最好的练习是什么?理想的情况下,我希望所有的属性,货币化,接受任何小数分隔符,逗号或点。这也是赚钱的方式:
pry(main)> Monetize.parse "2050.12"
=> #<Money fractional:205012 currency:USD>
pry(main)> Monetize.parse "2050,12"
=> #<Money fractional:205012 currency:USD>这太完美了。我如何配置我的模型(或货币化创业板在一般情况下)接受这两个参数(点或逗号)。
发布于 2020-05-29 09:29:33
希望这对某人有用。
型号:
monetize :rental_amount_cents, allow_nil: true查看:
= f.input :rental_amount, label: 'Rental amount' do
.input-group
= text_field_tag :rental_amount, @rental_period.rental_amount, class: 'form-control', id: "#{@rental_period.new_record? ? '' : (@rental_period.id.to_s + '_')}rental_amount_rate_rendered"
= f.hidden_field :rental_amount, class: 'rate-input'
%span.input-group-addon €Javascript设置:
$('[id$=rate_rendered]').add('.flex-rate').autoNumeric('init', settings.money_nosign).on('keyup', function() {
var $hid;
$hid = $(this).parent().find('input.rate-input');
if ($(this).autoNumeric('get') !== '') {
return $hid.val($(this).autoNumeric('get').replace('.', ','));
} else {
return $hid.val(0);
}
});在我的设置中(只有相关部分):
window.settings = {
money_nosign: {
aDec: ',',
aSep: '.',
vMin: '-999999999.99'
}
};https://stackoverflow.com/questions/46754895
复制相似问题