2016-07-08 21 views
1

我有這樣的結構在我的項目:鳳凰和嵌套content_tags有多個孩子刪除嵌套內容

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"> 
    &times; 
    </button> 
    <button class="close" type="button"> 
    &times; 
    </button> 
    <span><b>Some bold text</b>and nothing more</span> 
</div> 

但是,它給了我意想不到的事情(爲了提高可讀性,我添加了新行 - 原文中的所有內容都在一行中):

<div class="some-class" role="alert"> 
    <button class="close" type="button"> 
    &lt;span&gt;&lt;b&gt;Some bold text&lt;/b&gt;and nothing more&lt;/span&gt; 
    </button> 
</div> 

我不太明白,如何將兩個嵌套content_tag s合併成一個:safe字符串,同時使這個字符串"<span><b>Some bold text</b>and nothing more</span>"安全並且不會被轉義。

回答

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, ["&times;"]} 
    end 
    end, 
    content_tag(:button, type: :button, class: "close") do 
    content_tag(:span, class: "some-other-class") do 
     {:safe, ["&times;"]} 
    end 
    end, 
    {:safe, ["<span><b>Some bold text</b>and nothing more</span>"]}] 
end