开发者

Rails Undefined Method when Trying to Divide

开发者 https://www.devze.com 2023-03-06 10:01 出处:网络
I have what should be a simple operation where I want to calculate the percentage completion of something in Rails 3.

I have what should be a simple operation where I want to calculate the percentage completion of something in Rails 3.

In my Object model, I have the following function:

def balance
  (self.amount_remaining / self.amount) * 100
end

开发者_Python百科Then, in my Object view, I have the following:

<%= Object.balance %>

However, I get the following error:

undefined method `/' for nil:NilClass

Does anyone know what may be causing this error? Should I be making this calculate elsewhere?


Like this?

(self.amount_remaining / self.amount) * 100 rescue 0
0

精彩评论

暂无评论...
验证码 换一张
取 消