0

I have a users weight and height and now wish to calculate their BMI.

The formula is: divide weight in kilograms by the height in metres (m) then divide the answer by the height again.

So the user weight is stored in kgs:

<%= @user.weights.last.weight %> 

Whilst the user height is stored in cm:

<%= @user.height %>

So to print the BMI, any ideas where I would start. I just can't get my head around the syntax.

1 Answer 1

0

This is probably a method best done in the User model.

def get_bmi
  bmi = (self.weight/self.height)/self.height
end
5
  • So, should I include this is in Models/user.rb def get_bmi bmi = (self.weights.last.weight/self.height)/self.height end How would I then print it in the view? Jun 29, 2016 at 20:05
  • Why would a user have multiple weights? Jun 29, 2016 at 20:15
  • But to answer your question, you would call this in the view with <%= @user.get_bmi %> Jun 29, 2016 at 20:16
  • It is for a (very basic) weight loss app so the weight is always fluctuating. Jun 29, 2016 at 20:19
  • Getting error: 'String can't be coerced into Float' Jun 29, 2016 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.