2013-08-23 98 views
0

我有表格項目,其中有字符串「USD」或「EUR」。
我在控制變量創建
計算不同貨幣的付款

def index 
    ... 
    @currencydebt = CurrencyRate.find(:all, 
    :conditions => ["created_at < ? and currency_id = ?", Date.today, 2], 
    :order => ["created_at DESC"], :limit => 1) 
    ... 
    end 

巫找到最後的貨幣美元

**在我看來**

<% for res in @items %> 
    <% for loc in @currencydebt %> 
     <%= number_with_precision(((loc.rate.round(2) * res.payment)), 2) %> 
    <% end %> 
<% end %> 

Аnd作爲顯示貨幣美元paymant結果。
但是,如果貨品中有字符串美元,我怎麼做纔會顯示付款結果爲美元,如果貨品中有歐元支付結果爲歐元?

我的ActiveRecord

create_table "items", :force => true do |t| 
    t.string "name" 
    t.string "currency" 
    t.decimal "payment" 
    ... 
end 

create_table "currency_rates", :force => true do |t| 
    t.integer "currency_id" 
    t.decimal "rate" 
    ... 
end 

create_table "currencies", :force => true do |t| 
    t.string "name"  #USD or EUR 
    t.string "short_name" 
end 
+0

看看這個錢寶石和谷歌貨幣:http://stackoverflow.com/questions/10199301/rails-3-1-multiple-currencies或http://stackoverflow.com/questions/1019939/ruby- on-rails-best-method-of-handling-currency-money – rmagnum2002

+0

不幸的是我使用rails 2.0,不使用gems – Andrew

回答

1

首先,處理貨幣始終是棘手。有些寶石可以緩解疼痛,如this

爲了解決您的直接問題,您必須首先確定基礎貨幣。因此,假設您想保持美元作爲基礎貨幣,那麼請使用該寶石在您想要的任何貨幣之間切換。

+0

tnx作爲答案,但不幸的是我使用rails 2.0 – Andrew