In my Rails 3 application I use textarea
to let users to write a new message in a forum.
However, when the message is displayed, all newlines look like spaces (there is no <br />
). Maybe there are other mismatch examples, I don't know yet.
I wonder what is the most appropriate way to deal with this.
I guess that the text that is stored in the database is OK (I see for example that <
is converted to <
), so the main problem is the presentation.
Are there build-in helper methods in Rails for this ?
(simple_format does something that looks similar to what I need, but it adds <p>
tags which I don't want to appear.)
If someone still gets redirected here and uses Rails 4: http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format
You can now specify the tag it gets wrapped in (defaults to p) like so:
The following helper preserves new lines as line breaks, and renders any HTML or Script (e.g Javscript) as plain text.
Use as so in views
I just used
white-space: pre-line
. So next line (\n) will render it.