Use html_safe method to Display Rich Text in Lesson

Notes


This lesson is all about the html_safe method. When a Lesson object's content attribute has it's text formatted by the rich-text editor what's actually happening is the editor is surrounding the text with the proper HTML tags to create the desired format. If we were to type the following text into our rich-text editor for the content attribute with the bold formatting:

This is my simple blog post.

it would become:

This is my <bold>simple</bold> blog post.

before it was saved.

We need to tell Rails that it's okay to read and render the content attribute as true HTML. Let's visit the Lesson object's show view located in our app/views/lessons directory. Once inside that file, we need to find the p tag where we're displaying the content attribute and just call the html_safe method.

<p>
   <%= @lesson.content.html_safe %>
</p>

Now if you visit the Lesson object's show page you will see all the formatting we made in the Trix editor!

Happy coding!!!!