Rails—嵌套的content_tag (:ul ->:li ->:div)呈现div html标记

时间:2023-01-22 20:31:55

I'm trying to nest content tags into a custom helper, to create something like this:

我试图将内容标签嵌套到自定义帮助器中,以创建如下内容:

<ul>
  <li>
    Microposts
    <div>O</div>
  </li>
</ul>

I have tried this helper:

我试过这个帮手:

def user_info(user)
   user_microposts = content_tag(:li,  ("Microposts" + " " + content_tag(:div, user.microposts.count.to_s)))
  content_tag(:ul, user_microposts)
end

but it renders the div html tags in the view:

但它在视图中呈现div html标记:

Microposts
<div>0</div>

I would be pleased to know how to manage this! Thank you

我很高兴知道如何处理这件事!谢谢你!

1 个解决方案

#1


1  

You need to mark the string as HTML-safe, like this:

您需要将字符串标记为HTML-safe,如下所示:

... "Microposts ".html_safe + content_tag(...

#1


1  

You need to mark the string as HTML-safe, like this:

您需要将字符串标记为HTML-safe,如下所示:

... "Microposts ".html_safe + content_tag(...