I have such structure in my project:
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
And expect it to generate such 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>
However, it gives me something unexpected (I've added new lines for readability - in original everything is in one line):
<div class="some-class" role="alert">
<button class="close" type="button">
<span><b>Some bold text</b>and nothing more</span>
</button>
</div>
I do not really understand, how to join two nested content_tags into one :safe string, at the same time making this string "<span><b>Some bold text</b>and nothing more</span>" safe and not be escaped.
Looks like I've almost figured it out. This code should look almost like this:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With