converting plain text to html
My rails 3 app receives emails. Some of them are plain text. When the app displays them to the user I want them to be properly formatted. In other word I want开发者_如何学Go to encode plain text into html. For example: "Hello\n\nHello" =>
Hello
Hello
(or something like it).Of course I can write my own 4 lines of code but I am sure those 4 lines have already be written, tested and wrapped in some nice method call.
I know I'm a little late, but I actually think the proper solution to this, at least within Rails, is to leverage the simple_format
helper method provided from ActionView::Helpers::TextHelper
.
Wrap your text in a Pre tag:
<%= content_tag('pre', "Hello\n\nHello") %>
Using #html_safe
, let me explain with an example:
If in your controller the variable is:
@str = "<h1>Hi</h1>"
Then in the view:
<%= @str.html_safe %>
@Batkins has the right answer, which should be accepted. if someone who is still looking,
Converting plain text to HTML
<%= simple_format("plain text") %>
Converting HTML to proper plain text
text.html_safe
The simple_format is TextHelper module so you if you want to use simple_format method in controller
include ActionView::Helpers::TextHelper
in your controller
render :text => "bla bla bla"
it be useful http://apidock.com/rails/ActionView/Rendering/render
精彩评论