开发者

How to have Rails Views process HTML tags from database content?

开发者 https://www.devze.com 2023-02-02 06:43 出处:网络
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.

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.

Basically, I have a model named Page.

rails g model Page name:string content:text
开发者_如何学Python

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.

For example, in my Page#show view, I may have:

<%= @page.content %>

Which outputs:

< p >This is my first < b >paragraph< /b >.< /p >

When I want it to output:

This is my first paragraph.

Again, please be gentle. I know this is probably pretty basic but I am having a hard time searching for how to do this. :)


You can use the raw helper method.

<%=raw @page.content %> 


<%= @page.content.html_safe %> if you are safe with it's content.

0

精彩评论

暂无评论...
验证码 换一张
取 消