Ruby on rails 货币可以';不要被强迫浮动
我有一行代码引起头痛Ruby on rails 货币可以';不要被强迫浮动,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一行代码引起头痛 @amount = Currency.convert(current_user.currency, "USD", transaction.amount) 它会带来这样的错误 Currency can't be coerced into Float 应用程序跟踪如下所示 app/models/currency.rb:8:in `*' app/models/currency.rb:8:in `convert' app/controllers/paypal_controlle
@amount = Currency.convert(current_user.currency, "USD", transaction.amount)
它会带来这样的错误
Currency can't be coerced into Float
应用程序跟踪如下所示
app/models/currency.rb:8:in `*'
app/models/currency.rb:8:in `convert'
app/controllers/paypal_controller.rb:35:in `create'
class Currency < ActiveRecord::Base
attr_accessible :currency, :value
def self.convert(from,to,amount)
from_rate = Currency.find_by_currency(from)
to_rate = Currency.find_by_currency(to)
usd_value = (amount/(from_rate.value.to_f))
result = (usd_value.to_f * (to_rate.value.to_f))
return result
end
end
其中Currency.convert
模型方法如下所示
def self.convert(from,to,amount)
from_rate = Currency.find_by_currency(from)
to_rate = Currency.find_by_currency(to)
usd = (amount/(from_rate.value.to_f))
result = (usd*(to_rate.value.to_f))
return result
end
可能是什么错误
在控制台中运行
def convert(from,to,amount)
from_rate = Currency.find_by_currency(from)
to_rate = Currency.find_by_currency(to)
usd = (amount/(from_rate.value.to_f))
result = (usd*(to_rate.value.to_f))
return result
end
然后,
转换(“UGX”,“KES”,2000)。到四舍五入(0
)
工作
货币模型如下所示
app/models/currency.rb:8:in `*'
app/models/currency.rb:8:in `convert'
app/controllers/paypal_controller.rb:35:in `create'
class Currency < ActiveRecord::Base
attr_accessible :currency, :value
def self.convert(from,to,amount)
from_rate = Currency.find_by_currency(from)
to_rate = Currency.find_by_currency(to)
usd_value = (amount/(from_rate.value.to_f))
result = (usd_value.to_f * (to_rate.value.to_f))
return result
end
end
class Currency
大概是这一行的问题-结果=(usd*to_rate.value)
。如果您将其重写为result=(usd*(to_rate.value))`如果您将打印到_rate.value
,您会得到什么?usd和to_rate.value
的类别是什么?当您不期望时,其中一个可能是Currency
对象。@Teeg这些只是在方法中分配的变量。@user2963716-您能为我们提供货币模型的完整代码吗?或者至少是行号?@Chandranshu,我加上了。看一看。