I'm sorry if this looks like a very easy (too easy) question but I'm struggling to find a way into working this out.
I need to calculate the Basal Metabolic Rate according to the data enter into a Body model and render it in my show view. The problem is that I can run the calculation in the view but, of course, this is not a proper way.
<%= ((13.397*@user.bodies.last.weight)+(479.9 * @user.bodies.last.height)-(5.677 * @user.bodies.last.age)+(88.362)) * (1 - (@user.bodies.last.fat / 100.00)) %>
this code snippet is in a partial that feeds Body#show view. Of course, I want to have this done at the controller level.
So I wrote a method in my bodies_controller that looks like this:
# Calculate Basal Metabolic Rate for Males
def bmr
@user = User.find(params[:user_id])
@bmr = ((13.397 * @user.bodies.last.weight) + (479.9 *
@user.bodies.last.height) - (5.677 * @user.bodies.last.age) +
88.362) * (1 - (@user.bodies.last.fat / 100.00))
end
When trying to pull the result of this simple calculation into my Body#show view like this: <%= @bmr %>
nothing shows. Furthermore typing @bmr into the console returns NIL. How do I feed the Body attributes (Weight, age, height....) into the method?
Sorry if this question sounds stupid but I'm still learning.
Thanks a million in advance!