Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I allow Multi-line content in a rails text_area field?

How would I go about allowing users to input multi-line text in a text_area form field?

Currently anytime content is entered in the text areathe line breaks are removed... They are retained when I edit the content... So, maybe it's in the show view that the breaks are being removed?

I've found a few things that indicate that I'd need to implement a WYSIWYG editor, but I'd prefer to avoid that if possible.

Any suggestions?

UPDATE: So, it looks like rails saves the content with line breaks, and I can add .html_safe to the field in the show view (i.e. <%= @post.post.html_safe %>). Now it will present content with basic html (e.g. <br>, <b>, etc...) but still doesn't present the line breaks in the content (i.e. when I press enter for a new line) that are saved by rails and I can view when I'm editing the content.

like image 323
Joe Saunders Avatar asked Dec 28 '12 19:12

Joe Saunders


1 Answers

Linebreaks are not rendered in HTML. You will want to use something like simple_format or wraptext on your content as you echo it out to convert newlines to <br> or <p> tags.

<%=simple_format(@post.post).html_safe %>

or

<%=Wraptext::Parser.new(@post.post).to_html.html_safe %>
like image 196
Chris Heald Avatar answered Nov 02 '22 04:11

Chris Heald