Matteo Melani
Matteo Melani

Reputation: 2726

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 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.

Upvotes: 5

Views: 11292

Answers (5)

gsumk
gsumk

Reputation: 891

@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

Upvotes: 1

Felix Xilef
Felix Xilef

Reputation: 19

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 %>

Upvotes: 2

Batkins
Batkins

Reputation: 5706

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.

Upvotes: 23

user324312
user324312

Reputation:

Wrap your text in a Pre tag:

<%= content_tag('pre', "Hello\n\nHello") %>

Upvotes: 4

zloydadka
zloydadka

Reputation: 160

in your controller

render :text => "bla bla bla"

it be useful http://apidock.com/rails/ActionView/Rendering/render

Upvotes: 0

Related Questions