如何获得全价宝石
我正在使用 money gem,我想知道如何获得像 23.45、10.00 这样的价格, 0.32 在一个字段中显示为dollar.cents? 我希望我的cents 字段变成只是价格(金钱或美元和美分)。
这是我的代码:
我的模型与金钱宝石组成:
class Price < ActiveRecord::Base
attr_accessible :name, :date, :cents, :currency
belongs_to :user
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :currency, :presence => true
validates :cents, :presence => true
validates :date, :presence => true
default_scope :order => 'prices.created_at DESC'
composed_of :price,
:class_name => "Money",
:mapping => [%w(cents 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
表格(为了简洁起见,我剪掉了部分):
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :cents %><br />
<%= f.text_field :cents %>
</div>
<><div class="field">
<%= f.label :currency %><br />
<%= f.select(:currency,major_currencies(Money::Currency::TABLE),
{:include_blank => 'Select a Currency'}) %>
</div>
我的迁移或价格表:
class CreatePrices < ActiveRecord::Migration
def self.up
create_table :prices do |t|
t.integer :user_id
t.string :name
t.date :date
t.integer :cents, :default => 0
t.string :currency
好像我缺少一个专栏或其他东西。它甚至应该是单独的美分吗?不确定,所以我需要你的帮助。
感谢您抽出时间。
i am using the money gem and i wanted to know how i can get prices like 23.45, 10.00, 0.32 to show in one field as dollar.cents? I want my cents field to become just Price (money or dollars & cents).
Here is my code:
My model with money gem composed_of:
class Price < ActiveRecord::Base
attr_accessible :name, :date, :cents, :currency
belongs_to :user
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :currency, :presence => true
validates :cents, :presence => true
validates :date, :presence => true
default_scope :order => 'prices.created_at DESC'
composed_of :price,
:class_name => "Money",
:mapping => [%w(cents 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
The form (i cut out parts for brevity):
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :cents %><br />
<%= f.text_field :cents %>
</div>
<><div class="field">
<%= f.label :currency %><br />
<%= f.select(:currency,major_currencies(Money::Currency::TABLE),
{:include_blank => 'Select a Currency'}) %>
</div>
My migration or Price Table:
class CreatePrices < ActiveRecord::Migration
def self.up
create_table :prices do |t|
t.integer :user_id
t.string :name
t.date :date
t.integer :cents, :default => 0
t.string :currency
It seems like i am missing a column or something. It is even suppose to be cents alone? Not sure so i need your help.
Thank you for taking the time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
我下载了 gem 并测试了一些选项。按照 https://github.com/RubyMoney/money 中的示例,您可以这样做:
money = Money.new(1000, "USD")
money.cents #=> 1000
money.dollars #=> 10.0
所以在我看来你应该在你的模型 Price: 中有类似这个方法的东西,
def value
cents / 100.0
end
并在视图中使用它(或者任何你需要货币价值,而不是美分的地方)。添加价格时,您是以美分为单位的,因此要获得 12.34 的价格,您应该将其设置为 1234 美分。如果你想添加像 12.34 这样的价格,我想你应该在控制器中解析它(在创建/更新操作上),或者在带有过滤器 before_save 或类似内容的模型中解析它。只需将其乘以 100 即可在 DB 中保存为美分。
编辑:
向视图添加零的辅助方法。
def with_zeros(price)
array = price.to_s.split('.')
array[1] = array[1].ljust(2,'0')
array.join('.')
end
好的,我已经弄清楚了,谢谢大家!
这是我的新代码:
数据库现在是:价格而不是:美分 -
create_table :prices do |t|
t.integer :user_id
t.string :name
t.date :date
t.integer :price, :default => 0
t.string :currency
表单区域:
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
新的composed_of:
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 conver #{value.class} to Money") }
我只需制作它,以便composed_of匹配映射、数据库列和表单< /strong> 由于某种奇怪的原因它开始完全工作。感谢大家的帮助,我学到了很多关于 ruby 和 Rails 的知识。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
刚刚遇到了完全相同的问题,如果您不想像在所选答案中那样更改数据库,只需查看 gem 的源代码,看看是否有任何可以使用的内容:
< a href="https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/helpers/action_view_extension.rb" rel="nofollow noreferrer">https://github.com/RubyMoney /money-rails/blob/master/lib/money-rails/helpers/action_view_extension.rb
在这个模块中,您会注意到两件事:
因此,只需调用 @my_money_object.format,然后......bam,您的货币对象格式就变成了包含美元和美分金额的字符串,精度为 2。
示例:
=> “$20.00”
这确实是正确的解决方案。您明确表示您“只是想要获得全价(例如 45.23 美元)的方法”。现在,如果您想像这样将这个格式化字符串放入数据库中,这取决于您,但我会考虑在需要时调用货币对象上的格式方法。我需要在控制器中获取格式化的价格,并且显然无法访问 humanize_money 方法,除非我在视图中(无需尝试破解其他方法来执行此操作),所以我检查了源代码并发现这。
Just ran into the exact same problem, and if you don't want to change your DB like you did in your chosen answer, simply take a look at the source code of the gem to see if there is anything that you can use:
https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/helpers/action_view_extension.rb
In this module, you will notice two things:
So, simply call @my_money_object.format, and....bam, you have your money object format to a string WITH the dollars, and the cents amount with a precision of 2.
Example:
=> "$20.00"
This is really the correct solution. You specifically said that you "just want the method on getting a full price like $ 45.23." Now if you want to put this formatted string in your DB like that, it is up to you, but I would consider just calling the format method on the money object where needed. I needed to get the formatted price in a controller and obviously didn't have access to the humanize_money method unless I am in a view (without trying to hack out an additional method to do this), so I checked out the source code and found this.