如何在 rails 中连接嵌套的 div 标签

How can concat nested div tag in rails

我尝试在 rails 中连接嵌套的 div 标签。

foo = ''
foo.concat content_tag :div do 
  label_tag 'bar' 
end
=> "<div></div>"

但它总是 returns div 没有内部标签的标签。

为什么会这样?

由于 ruby 作用域完整块传递给 concat 而不是单独 content_tag。您可以使用 + 运算符连接返回的字符串。试试下面的代码。

foo = ''
foo += content_tag :div do 
  label_tag 'bar' 
end
=> "<div><label for="bar">Bar</label></div>"