如何让 Rails 视图处理数据库内容中的 HTML 标签? [英] How to have Rails Views process HTML tags from database content?
问题描述
好的.我知道这是非常基本的.我想在数据库中的文本内容周围有一些基本标签,以便在视图的渲染中进行处理.
Ok. I know this is very basic. I want to have some basic tags around text content in the database to be processed in the view's rendering.
基本上,我有一个名为 Page 的模型.
Basically, I have a model named Page.
rails g model Page name:string content:text
我希望在视图中显示内容字段中的任何内容,但是,如果内容字段中有 HTML,我希望它被处理而不仅仅是显示.
I want whatever is in the content field to be shown in the view but, if there is HTML in the content field, I want it to be processed not just shown.
例如,在我的 Page#show 视图中,我可能有:
For example, in my Page#show view, I may have:
<%= @page.content %>
输出:
这是我的第一个 <b >段落.
< p >This is my first < b >paragraph< /b >.< /p >
当我想要它输出时:
这是我的第一个段落.
再次,请温柔.我知道这可能是非常基本的,但我很难寻找如何做到这一点.:)
Again, please be gentle. I know this is probably pretty basic but I am having a hard time searching for how to do this. :)
推荐答案
您可以使用 raw 辅助方法.
You can use the raw helper method.
<%=raw @page.content %>
这篇关于如何让 Rails 视图处理数据库内容中的 HTML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!