2013-08-23 89 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