如何在 Rails 中解析 HTML?

发布于 2024-10-05 02:14:04 字数 368 浏览 8 评论 0原文

我保存了一个带有 html 标签的字符串。

=> "<p>hey man this is crazy g funk</p>\n<p>here i come with another crazy message from..</p>\n<p>dj eassssy d!@#!.</p>"

您如何解析它以使其显示 HTML 标记所暗示的方式?

我试过:

= Post.text
=h Post.text
= RedCloth.new(Post.text).to_html
= Hpricot(Post.text)

I have a string with html tags in it saved.

=> "<p>hey man this is crazy g funk</p>\n<p>here i come with another crazy message from..</p>\n<p>dj eassssy d!@#!.</p>"

How do you parse this so that it displays the way the HTML tags are implying?

I tried:

= Post.text
=h Post.text
= RedCloth.new(Post.text).to_html
= Hpricot(Post.text)

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

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

发布评论

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

评论(2

尹雨沫 2024-10-12 02:14:05

一般来说,我们使用 html 解析器来解析 html。 “以便它显示 HTML 标签所暗示的方式”是什么意思?显示在什么上?大概不是网络浏览器..

Generally one parses html with html parsers. What do you mean "so that it displays the way the HTML tags are implying"? Displays on what? Presumably not a web browser..

醉南桥 2024-10-12 02:14:04

你想这样做:

<%= raw Post.text %>

或者在 haml 中

= raw Post.text

原因是因为rails转义了你的html并将

转换为 <p>

You want to do this:

<%= raw Post.text %>

or in haml

= raw Post.text

The reason is because rails escapes your html and will convert <p> into <p>.

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