我正在製作一個樣式指南,其中我在左側顯示的右側輸出代碼。將ERB作爲HTML和ERB從Rails視圖呈現
我知道加入%%逃脫ERB
我寫了一個幫手,需要一個塊的內容,並呈現代碼在兩個地方一個顯示HTML和我想讓對方顯示源ERB是創建了html。
問題是我找回了我想要ERB的HTML。
視圖代碼
<%= display_code do %>
<%= link_to "Button", "/style_guide, class: "btn" %>
<% end %>
助手代碼
module StyleGuideHelper
def display_code(&block)
content = with_output_buffer(&block)
html = ""
html << content_tag(:div, content, class: "rendered-code")
html << content_tag(:div, escape_erb(content), class: "source-code-preview")
html.html_safe
end
def escape_erb(code)
code = code.gsub("%=", "%%=")
end
end
預期結果 按鈕<%=的link_to 「按鈕」, 「/ style_guide,等級:」 BTN 「%>
實際結果 按鈕按鈕
乾杯