1

So I lets say I have a variable that holds some HTML elements. How do I get that to display correctly?

Example

@mainArticle = "<p>Hey this is an example</p>"

//HTML Page

<div class="mainArticleSpace">
     <%= @mainArticle %>
</div>

I've tried this and it includes the p tags when you try to display.

user3591126
  • 211
  • 1
  • 8

1 Answers1

4

Only show allowed html tags and attributes

You can sanitize the content to make sure only safe tags and attributes are accepted.

<%= sanitize @mainArticle %>

And if you want to specify the tags yourself you can do it as well.

<%= sanitize(@mainArticle, tags: %w(p h1), attributes: %w(id class)) %>

Check out the rails sanitize helper documentation for more options and how to configure it.

Remove all html tags

You can also use strip_tags if you really want to get rid of all html tags.

<%= strip_tags @mainArticle %>

Check out the strip_tags documentation

Marc Lainez
  • 3,070
  • 11
  • 16