如何获得全价宝石

发布于 11-13 12:08 字数 2131 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初见你2024-11-20 12:08:17

刚刚遇到了完全相同的问题,如果您不想像在所选答案中那样更改数据库,只需查看 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

在这个模块中,您会注意到两件事:

  1. 一个名为 humanize_money 的方法
  2. 在该方法中,对方法格式的调用货币对象

因此,只需调用 @my_money_object.format,然后......bam,您的货币对象格式就变成了包含美元和美分金额的字符串,精度为 2。

示例:

@my_money_object = 20.to_money
@my_money_object.format

=> “$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:

  1. A method named humanize_money
  2. Within that method, a call to the method format on a money object

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:

@my_money_object = 20.to_money
@my_money_object.format

=> "$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.

好菇凉咱不稀罕他2024-11-20 12:08:17

好吧,

在您的数据库表中,您将美分保存为整数:

 t.integer :cents, :default => 0

尝试将其更改为:

 t.decimal :cents, :default => 0

Well,

In your DB table you hold cents as integers:

 t.integer :cents, :default => 0

Try changing that to:

 t.decimal :cents, :default => 0
一口甜2024-11-20 12:08:17

我下载了 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

I downloaded the gem and tested a few options. Following the examples from https://github.com/RubyMoney/money you can do:

money = Money.new(1000, "USD")
money.cents     #=> 1000
money.dollars   #=> 10.0

So in my opinion you should have something like this method in your model Price:

def value
  cents / 100.0
end

And use it in the views (or wherever you need value in currency, not cents). When adding a price you are doing it in cents, thus to have a price 12.34 you should set it to 1234 cents. If you want to add price like 12.34, I guess you should parse it in controller (on create/update action) or in the model with filter before_save or something like that. Just multiply it by 100 to save as cents in DB.

EDIT:
helper method to add zeros to view.

def with_zeros(price)
  array = price.to_s.split('.')
  array[1] = array[1].ljust(2,'0')
  array.join('.')
end
﹏雨一样淡蓝的深情2024-11-20 12:08:17

好的,我已经弄清楚了,谢谢大家!

这是我的新代码:

数据库现在是:价格而不是:美分 -

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 的知识。

Alright i was able to figure it out, thanks everyone!

Here is my new code:

Database now is :price instead of :cents -

create_table :prices do |t|
  t.integer :user_id
  t.string :name
  t.date :date
  t.integer :price, :default => 0
  t.string :currency

form area:

  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>

new 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") }

I just had to make it so the composed_of matches the mapping, database column and form and for some strange reason it starting working entirely. Thanks everyone for the help, i learned a great deal about ruby and the rails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文