Reputation: 147
Currently I'm working with Rails.
I have erb form and what I want to do is showing the result of function in controller to erb
def total_ratings
total_ratings = 1000
Score.where('ratings_count > ?', 0).each do |score|
total_ratings += score.ratings_count
end
render total_ratings
end
After I get total_ratings as number,
I tried to show the total_ratings like this in erb form
<div class="page-header-description">ratings : <%= score_total_ratings_scoring_index_path %></div>
But this only printed out the path, not the result. Which point I missed in here?
Upvotes: 1
Views: 79
Reputation: 44380
Move your function from controller to the helper module to render in in the view.
# app/helpers/application_helper.rb
module ApplicationHelper
def total_ratings
total_ratings = 1000
Score.where('ratings_count > ?', 0).each do |score|
total_ratings += score.ratings_count
end
total_ratings
end
end
Now render it in the view:
<div class="page-header-description">ratings : <%= total_ratings %></div>
Read more about Rails helpers
Upvotes: 3