Nuno Silva
Nuno Silva

Reputation: 758

Put string with markup text inside view

So I have this string:

"<p> Here it is: </p> <p> <a href="http://localhost:3000/" class="button">Open</a> </p>"

This string is generated inside the controller, depending on several parameters.

I want to be able to show this string in a view, interpreted as markup text:

<div class="row">
  <div class="twelve columns panel">
    <%= @string_with_markup %>
  </div>
</div>

But the result is that, not surprisingly, the string is displayed, as is, so it shows a bunch of markup text in the rendered page. I though of render_to_string or yield methods, but its not working, I think I'm missing something.

Additional info:

The whole purpose is to show the body of an email sent by the system. The emails are generated with ActionMailer, and when the user wants to see an email that was sent to him, the controller calls ActionMailer's appropriate method, and extracts the body of the email. That string_with_markup I talked about is in fact of class: Mail::Body

Thanks!

Upvotes: 2

Views: 460

Answers (2)

Stefan
Stefan

Reputation: 114158

You can mark this string (html) safe in your controller (or helper):

@string_with_markup = '<p>Here it is ...</p>'.html_safe

And render it using:

<%= @string_with_markup %>

If you prefer not to mark this string safe in your controller, use raw or <%== rather than calling html_safe in your view:

<%= raw @string_with_markup %>

which is equivalent to:

<%== @string_with_markup %>

See http://guides.rubyonrails.org/v3.2.9/active_support_core_extensions.html#output-safety

Upvotes: 1

jmif
jmif

Reputation: 1212

Try this:

<div class="row">
  <div class="twelve columns panel">
    <%= @string_with_markup.html_safe %>
  </div>
</div>

Upvotes: 1

Related Questions