Rails 3 视图中的 Html 转义

发布于 2024-09-16 09:15:15 字数 259 浏览 8 评论 0原文

我正在使用 Rails 3。我想在 erb 模板

<%= "<div>Foo Bar</div>" %>

Rails 编码 div 标签内显示生成的 html 片段。

如果我在 Rails 2 中是正确的 <%=h 会导致 html 转义。似乎Rails 3中已更改。如何在Rails 3中插入不编码的html片段?

问候, 阿列克谢.

I'm using Rails 3. I want to display generated html fragment inside erb template

<%= "<div>Foo Bar</div>" %>

Rails encodes div tags.

If I'm correct in Rails 2 <%=h causes html escaping. Seems that it was changed in Rails 3. How can insert html fragment without encoding in Rails 3?

Regards,
Alexey.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-09-23 09:15:16

我假设编码指的是 html 转义:

要在 Rails 3 中输出原始 html,您可以使用三种不同的方法。

  1. 您可以使用raw帮助器输出原始html

    <% some_string = "
    Hello World!
    " %> <%= some_string %> <%=原始 some_string %>

    更多信息:ActionView::Helpers::OutputSafetyHelper #raw

  2. 您可以将字符串标记为 html_safe

    <% some_string = "
    Hello World!
    ".html_safe %>; <%= some_string %>

    更多信息:String#html_safeActiveSupport::SafeBuffer#new

  3. < p>您可以使用 sanitize 清理输出

    <%=清理“
    Hello World!
    ”,标签:%w( div ) %>

    更多信息:ActionView::Helpers::SanitizeHelper #sanitze

更多信息:

I assume by encoding you mean the html-escaping:

To put out raw html in Rails 3 you can use three different approaches.

  1. your can use the raw helper to output raw html

    <% some_string = "<div>Hello World!</div>" %>
    <%= some_string %>
    <!-- outputs: <div>Hello Worlds!</div> -->
    <%=raw some_string %>
    <!-- outputs: <div>Hello Worlds!</div> -->
    

    more information: ActionView::Helpers::OutputSafetyHelper#raw

  2. You can mark the string as html_safe

    <% some_string = "<div>Hello World!</div>".html_safe %>
    <%= some_string %>
    <!-- outputs: <div>Hello World!</div> -->
    

    more information: String#html_safe and ActiveSupport::SafeBuffer#new

  3. You can sanitize your output with sanitize

    <%=sanitize "<div>Hello World!</div>", tags: %w( div ) %>
    

    more information: ActionView::Helpers::SanitizeHelper#sanitze

Some more Information:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文