带有Rails 4的Money-Rails宝石-实例货币 [英] Money-Rails Gem with Rails 4 - Instance Currencies

查看:74
本文介绍了带有Rails 4的Money-Rails宝石-实例货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Rails 4应用程序(带有money-rails gem)中使用获利.

Im trying to use monetise in my Rails 4 app (with money-rails gem).

您如何允许用户仅提交数字美元?当我输入100时,我得到的是$ 1而不是$ 100.

How do you allow a user to submit a number dollars only? When I input 100 I get $1 instead of $100.

在我的模型中,我有:

monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency

我正在使用实例货币,因此用户选择相关货币.我的表格中有参与费用,参与费用便士和参与费用货币的列.

I am using instance currencies, so users select the relevant currency. My table has columns for participation cost, participation cost pennies and participation cost currency.

在我的表单中,我有:

   <%= par.select :participation_cost_currency,
                             options_for_select(major_currencies(Money::Currency.table)),
                             label: false,
                             prompt: "Select your costs currency" %>

            <%= par.input :participation_cost, label: false, placeholder: 'Whole numbers only', :input_html => {:style => 'width: 250px; margin-top: 20px', class: 'response-project'} %>

我认为,我有:

   <%= money_without_cents_and_with_symbol @project.scope.participant.participation_cost  %>

通过用表格中的参与成本替换参与成本便士",我得到的数字显示为不带美分的整数,当我输入100时,我现在得到10,000美元(因此,相反的问题是它将2 00加到我输入的结尾.

By replacing 'participation cost pennies' with participation cost in the form, I get the number to show as a whole number without cents I now get $10,000 when i enter 100 (so the reverse problem in that it is adding 2 00s to the end of my input.

推荐答案

似乎,您在数据库中使用了price列,并要求用户输入完全相同的输入.这两个不同的设置器/获取器.请尝试以下操作:

Seems, you use price column inside the database and ask users to input exactly same input. This two different setters/getters. Try the following:

# add migration
rename_column :products, :price, :price_cents


# set monetize for this field inside the model
class Product
  monetize :price_cents
end

# inside form use .price instead of .price_cents method
f.text_field :price

在这种情况下,用户将价格设置为美元,它将自动转换为美分,以将其存储在price_cents字段中.

In this case user set price in dollars and it will be automatically converted to the cents to store it inside price_cents field.

这篇关于带有Rails 4的Money-Rails宝石-实例货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆