Mark
Mark

Reputation: 6455

content_tag and html_safe not working as expected

I've been given a string with html tags present, and my end goal is getting this to display in a p block with class 'style1' after applying the formatting present in the html tags:

<p class="style1">Text I want to display after applying formatting present in tags</p>

I've tried the following methods:

<%= content_tag :p, class: 'style1' do %>
  <%= question.html_safe %>
<% end %>


<p class="style1"><%= question.html_safe %></p>


<%= content_tag :p, question.html_safe, class: 'style1' %>

All 3 of these approaches leave me with:

<p class="style1"></p>
<p> Formatted question text </p>

I can get the text to appear in the correct paragraph div if I use strip_tags(question), however this obviously removes any styling before presenting the data.

Is there any way I can apply the styling, not have the tags show up, AND get the content in the correct p tag?

Thanks in advance.

Upvotes: 1

Views: 465

Answers (1)

SRack
SRack

Reputation: 12203

Does the question string contain an additional p tag? If so, it won't work as you can't nest a p within a p.

Could you change the wrapper to a div? This should fix it. I.E.

<%= content_tag :div, question.html_safe, class: 'style1' %>

Or, strip any p tags from the question:

<%= content_tag :div, question.gsub(/<\/?p>/, '').strip.html_safe, class: 'style1' %>

Upvotes: 1

Related Questions