user3382438
user3382438

Reputation: 131

Rails - Render HTML output from Database

I have a working form which stores data in my database and I can get this data and display it on my site.

I now want to store HTML code directly in the database to enable styling. For example I want my title to have a h3 format. I can write this into my form and it will be stored to the database.

<h3>Test Title</h3>

But when I display the output of this form it shows the h3 tag in cleartext (of course). I want to know how to render this as html code? Can you help me?

Upvotes: 2

Views: 1404

Answers (2)

Rails Fan
Rails Fan

Reputation: 1144

You can do it like this:

<%= raw @html_source %>

I like to note that raw is more "user-friendly" than html_safe, because it silently output an empty string for nil too. While html_safe would throw

* NoMethodError Exception: undefined method `html_safe' for nil:NilClass

Upvotes: 4

tompave
tompave

Reputation: 12427

or you can declare the string as html_safe:

my_string.html_safe

Upvotes: 0

Related Questions