4

I have this in my helper:

  def favorites_count(node)
    content_tag :span, class: "card-favorite-count" do
      content_tag :i, class: "icon-heart"
      node.cached_votes_total
    end
  end

That is being called like this in the view:

<%= favorites_count(node) %>

And that renders this:

<span class="card-favorite-count"></span>

How do I get it to render the entire thing?

Edit 1

Per @tolgap's suggestion below, I tried this:

  def favorites_count(node)
    content_tag :span, class: "card-favorite-count" do
      content_tag(:i, "" ,class: "icon-heart") + node.cached_votes_total
    end
  end

But that doesn't output the number value in node.cached_votes_total. It outputs everything else though, and in the correct semantic order. It is just the final part of this doesn't work quite yet.

marcamillion
  • 32,933
  • 55
  • 189
  • 380

2 Answers2

3

The last expression in the do block for a content_tag is what the content will be. So change it to:

def favorites_count(node)
  content_tag :span, class: "card-favorite-count" do
    node.cached_votes_total + content_tag(:i, class: "icon-heart")
  end
end

So you concatenate those two together. Of course, you will need to do nil checks on node.cached_total_votes now.

marcamillion
  • 32,933
  • 55
  • 189
  • 380
tolgap
  • 9,629
  • 10
  • 51
  • 65
  • I got this error: `ActiveSupport::SafeBuffer can't be coerced into Fixnum` on that `node.cached_votes_total...` line. – marcamillion Jul 19 '15 at 09:41
  • Ok...I swapped them around. i.e. I did the `content_tag(:i, " ", class: "icon-heart") + node.cached_votes_total`. The issue is now I see the heart, but I don't see the content from `node.cached_votes_total`. In the HTML, it prints as `""`. – marcamillion Jul 19 '15 at 09:44
2

So I figured out the answer. This is what the correct solution looks like:

  def favorites_count(node)
    content_tag :span, class: "card-favorite-count" do
      concat(content_tag(:i, "" ,class: "icon-heart"))
      concat(node.cached_votes_total)
    end
  end

Note that I had to use two concat() methods, because concat basically acts like a puts to the view. content_tag basically just returns the last line in the method to the view, so to override that I have to do that.

This comes from this article and this SO answer.

Community
  • 1
  • 1
marcamillion
  • 32,933
  • 55
  • 189
  • 380