如何在 Rails 中解析 HTML?

Tri*_*rip 1 html ruby-on-rails redcloth

我保存了一个带有 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>"
Run Code Online (Sandbox Code Playgroud)

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

我试过:

= Post.text
=h Post.text
= RedCloth.new(Post.text).to_html
= Hpricot(Post.text)
Run Code Online (Sandbox Code Playgroud)

jon*_*nii 5

你想要这样做:

<%= raw Post.text %>
Run Code Online (Sandbox Code Playgroud)

或在哈姆尔

= raw Post.text
Run Code Online (Sandbox Code Playgroud)

原因是因为 Rails 转义了您的 html 并将转换<p>&lt;p&gt;.