我正在制作一个样式指南,其中我在左侧显示的右侧输出代码。将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 “%>
实际结果 按钮按钮
干杯