2

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!

4

2 回答 2

1

您可以在控制器(或助手)中将此字符串 (html) 标记为安全:

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

并使用以下方式渲染它:

<%= @string_with_markup %>

如果您不想在控制器中将此字符串标记为安全,请使用raw或在您的视图中<%==调用:html_safe

<%= raw @string_with_markup %>

这相当于:

<%== @string_with_markup %>

请参阅http://guides.rubyonrails.org/v3.2.9/active_support_core_extensions.html#output-safety

于 2013-05-11T18:34:07.450 回答
1

尝试这个:

<div class="row">
  <div class="twelve columns panel">
    <%= @string_with_markup.html_safe %>
  </div>
</div>
于 2013-05-11T18:07:18.303 回答