TypeError (String can't be coerced into Fixnum)?

前端 未结 1 446
情深已故
情深已故 2021-01-18 11:02

I\'m getting a weird error. My app runs perfectly fine on my localhost but on my Heroku server it\'s giving this error: TypeError (String can\'t be coerced into Fixnum

相关标签:
1条回答
  • 2021-01-18 11:39

    Ruby uses + as a combination tool for strings AND mathematical situations.

    Simple fix:

    def rep_score(user)
     rep = 0
     user.badges.each do |b|
       rep = rep + b.rep_bonus.to_i
     end
     return rep
    end
    

    to_i is changing rep_bonus, which is probably from the database model, from a string result into an integer. There are a few different typecasts you can set. To name a few conversions:

    • Array: to_a
    • Float: to_f
    • Integer: to_i
    • String: to_s
    0 讨论(0)
提交回复
热议问题