qanknku
qanknku

Reputation: 147

showing result to erb from controller

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

Answers (1)

Roman Kiselenko
Roman Kiselenko

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

Related Questions