How to have Rails Views process HTML tags from dat

2020-02-20 07:09发布

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

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. :)

2条回答
叼着烟拽天下
2楼-- · 2020-02-20 07:56

You can use the raw helper method.

<%=raw @page.content %> 
查看更多
家丑人穷心不美
3楼-- · 2020-02-20 08:06

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

查看更多
登录 后发表回答