2

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?

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

or you can declare the string as html_safe:

my_string.html_safe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.