Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 货币可以';不要被强迫浮动_Ruby On Rails_Ruby - Fatal编程技术网

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,我加上了。看一看。