我的项目中有这样的结构:
content_tag(:div, class: "some-class", role: "alert") do
content_tag(:button, type: :button, class: "close") do
content_tag(:span, class: "some-other-class") do
{:safe, ["×"]}
end
end
content_tag(:button, type: :button, class: "close") do
content_tag(:span, class: "some-other-class") do
{:safe, ["×"]}
end
end
"<span><b>Some bold text</b>and nothing more</span>"
end
并期望它生成这样的HTML:
<div class="some-class" role="alert">
<button class="close" type="button">
×
</button>
<button class="close" type="button">
×
</button>
<span><b>Some bold text</b>and nothing more</span>
</div>
然而,它给了我一些意想不到的东西(我已添加了新的可读性线 - 原来一切都在一行):
<div class="some-class" role="alert">
<button class="close" type="button">
<span><b>Some bold text</b>and nothing more</span>
</button>
</div>
我真的不明白,如何将两个嵌套的content_tag
加入一个:safe
字符串,同时使这个字符串"<span><b>Some bold text</b>and nothing more</span>"
安全无法转义。
答案 0 :(得分:3)
看起来我差不多想通了。这段代码看起来应该是这样的:
content_tag(:div, class: "some-class", role: "alert") do
[content_tag(:button, type: :button, class: "close") do
content_tag(:span, class: "some-other-class") do
{:safe, ["×"]}
end
end,
content_tag(:button, type: :button, class: "close") do
content_tag(:span, class: "some-other-class") do
{:safe, ["×"]}
end
end,
{:safe, ["<span><b>Some bold text</b>and nothing more</span>"]}]
end