2011-06-07 61 views
3

我正在使用money gem,我想知道如何獲得像23.45,10.00,0.32這樣的價格在一個字段中顯示爲dollar.cents?我想我的仙田成爲只是價格(錢或美元&美分)。如何獲得完整的金錢寶石

這裏是我的代碼:

我用錢的寶石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 

的形式(切出簡潔部分):

<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 

看起來好像我缺少一列或其他東西。甚至假設只有美分?不知道,所以我需要你的幫助。

謝謝你抽出時間。

+1

也許這可能有助於:http://stackoverflow.com/questions/4798651/rails-money-gem-converts-all-amounts-to-zero/4800159 – apneadiving 2011-06-07 06:10:23

+0

只需看看它:你有DB和美分貨幣在視圖中(以其爲準),因此23.45以貨幣== 2345美分。 – santuxus 2011-06-07 09:47:43

+0

@apneadiving我實際上遵循了你接受的答案讓我開始,創造了這樣的一切,仍然得到了上述問題。 – LearningRoR 2011-06-07 13:30:21

回答

0

好吧我能弄明白,謝謝大家!

這是我的新代碼:

數據庫現在是:價格,而不是:美分 -

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匹配映射,數據庫列和形式和一些奇怪的原因,它開始工作完全。感謝大家的幫助,我瞭解了很多關於ruby和rails的知識。

1

好,

在你的數據庫表你持有美分整數:

t.integer :cents, :default => 0 

嘗試改變,爲:

t.decimal :cents, :default => 0 
+0

現在這個工程在一定程度上,我可以把23.45,但如果我把23.50它出來23.5而不是。 – LearningRoR 2011-06-07 13:57:58

+1

23.50和23.5完全一樣 - 如果你想在最後使用0,我認爲你必須用ruby格式化 – 2011-06-07 13:59:30

+0

你是對的,但是人們不會感到困惑,所以對於$ 23,50。 – LearningRoR 2011-06-07 14:22:12

1

我下載了寶石和測試的幾個選項。繼從https://github.com/RubyMoney/money的例子,你可以這樣做:

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

所以在我看來,你應該有這樣的事情的方法在你的模型價格:

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 
+0

我實際上不想以美分來做它,我只是遵循本網站上的教程和問題。我只想在一個領域獲得像45.23美元的全價的方法。 – LearningRoR 2011-06-07 14:26:45

+1

在這種情況下,我認爲Tudor Constantin提供了一個您正在尋找的解決方案...您可以添加一個輔助方法,在視圖中添加所需的零。 (我添加了上面的代碼 - 它以逗號後的數字返回字符串) – santuxus 2011-06-07 14:46:45

4

剛剛遇到完全相同的問題,如果您不想像您在所選答案中那樣更改數據庫,只需查看gem的源代碼以查看是否有任何您可以使用:

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方法,除非我處於視圖中(而不嘗試破解一個額外的方法來執行此操作),所以我檢出了源代碼並找到這個。如果這對你有任何幫助,請投票。

相關問題