为什么此嵌套的content_tag无法正确呈现? [英] Why does this nested content_tag not render properly?

查看:53
本文介绍了为什么此嵌套的content_tag无法正确呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的助手中有这个

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

在视图中这样调用:

<%= favorites_count(node) %>

然后呈现出来:

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

如何获取渲染整个内容的东西?

How do I get it to render the entire thing?

编辑1

根据下面的@tolgap的建议,我尝试了此操作:

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

但是,这不会在 node.cached_votes_total 中输出数字值.但是,它以正确的语义顺序输出其他所有内容.这只是最后一部分,目前还无法正常工作.

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.

推荐答案

所以我想出了答案.正确的解决方案如下所示:

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

请注意,我必须使用两个 concat()方法,因为concat基本上就像是视图的puts一样. content_tag 基本上只是将方法的最后一行返回到视图,因此要覆盖我必须这样做.

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.

这来自这篇文章和此 SO答案.

这篇关于为什么此嵌套的content_tag无法正确呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆