Samuel
Samuel

Reputation: 6136

Rails - How to move calculation from view to controller

Fairly new to RoR and wondering how I might move this one line of code out of my view and into my controller. I am also using devise and a current user is logged in. Thanks in advance

profile.html.erb

 <%= @profile.calorie / 4  %>

Ive set up a method called calculate within my profiles_controller.rb like so

 helper_method :calculate

 def calculate 
  ..... 
 end 

Upvotes: 1

Views: 939

Answers (1)

miahabdu
miahabdu

Reputation: 614

Just put the calculation in an instance variable in your controller and call it in your view:

<%= @new_calorie  %>

And in your controller action:

def show #or index
   @new_calorie = @profile.calorie / 4
end

Upvotes: 1

Related Questions