Rails -- Add a line break into a text area
Asked Answered
D

11

69

I got a rails app where I can input a few paragraphs of text into my model. The problem is I dont know how to input any line breaks.

I've tried to add " {ln}{/ln} ; {&nbsp} and {br}{/br}" but that only displays the html as text and no break.

Is there anyway I can set it so the text area control will use any of the html I place within the model entry?

Is there any thing I can type so rails will recognize, hey put a line here?

Daysidayspring answered 29/6, 2010 at 2:7 Comment(2)
Where does it not render correctly? Within the actual textarea when you go back to edit the value (i.e. <%= f.text_area :description %>)? Or on the page when you're rendering the value plainly (i.e. <%= obj.description %>)?Cramer
on the page where I'm trying to render the data it just places the < br > as textDaysidayspring
C
88

The problem isn't so much editing the value as it is rendering it later. To add newline characters to your value while editing it in a textarea, just hit the return key. When you re-edit that value later, the whitespace should still be there.

Rendering the whitespace is the tricky part. In HTML, whitespace is generally insignificant. A renderer like the one your browser uses will display a single space for any continuous string of whitespace. So merely dumping the value onto the page won't be enough:

<%= obj.description %>

Even though your value may be "One \t \n \n Two", it will show up on the screen as "One Two".

To get those new line characters to actually separate the lines when displayed, you'll need to convert them to HTML before rendering:

<%= obj.description.gsub(/\n/, '<br/>') %>

Of course, if users are entering data that will be included in your HTML, you should be escaping the values to protect against XSS. If new lines are the only thing you need to support, it should be as simple as this:

<%= h(obj.description).gsub(/\n/, '<br/>') %>

If you want to allow more complex formatting, look into Markdown and Textile (both of which Rails provides helper view methods for). Just be sure to investigate what if any support they provide for XSS prevention.

Cramer answered 29/6, 2010 at 4:36 Comment(1)
As of Rails 3, you'll have to add .html_safe: <%= h(obj.description).gsub(/\n/, '<br/>').html_safe %>Stilbestrol
P
114

Line breaks in textareas are produced as `\n'. However, the problem is that if you simply dump it into your view, it will just be line breaks in your HTML source.

You can try using the Rails simple_format helper to take care of some of this for you: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M002285

It will auto-convert line breaks to HTML tags. You can use it with something like <%= simple_format(my_text_field) %>.

Playground answered 30/6, 2010 at 0:54 Comment(4)
For the record, to avoid XSS attacks you should be escaping the var like this: <%= simple_format h(my_text_field) %>Sahara
@Sahara simple_format sanitizes by default, h() isn't needed. This is an old post...maybe this functionality is new.Stilbestrol
@Stilbestrol it's rails 3 specific, introduced in github.com/rails/rails/commit/… - 2010/06/17; Either I didn't know or assumed rails 2.x (can't remember). Thanks for mentioning it for the following readers! :)Sahara
The problem with simple_format is that it is too clever. It will preserve any \n in your text, but it will assume that two \n's are meant to indicate a new paragraph and will take your input and wrap specific parts of it in <p> tags. It really depends on what your needs are which method you should use. If you absolutely must preserve all new lines, you should use the accepted answer. Otherwise use simple_format and plan for paragraph tags where you'll be outputting this value.Riannon
C
88

The problem isn't so much editing the value as it is rendering it later. To add newline characters to your value while editing it in a textarea, just hit the return key. When you re-edit that value later, the whitespace should still be there.

Rendering the whitespace is the tricky part. In HTML, whitespace is generally insignificant. A renderer like the one your browser uses will display a single space for any continuous string of whitespace. So merely dumping the value onto the page won't be enough:

<%= obj.description %>

Even though your value may be "One \t \n \n Two", it will show up on the screen as "One Two".

To get those new line characters to actually separate the lines when displayed, you'll need to convert them to HTML before rendering:

<%= obj.description.gsub(/\n/, '<br/>') %>

Of course, if users are entering data that will be included in your HTML, you should be escaping the values to protect against XSS. If new lines are the only thing you need to support, it should be as simple as this:

<%= h(obj.description).gsub(/\n/, '<br/>') %>

If you want to allow more complex formatting, look into Markdown and Textile (both of which Rails provides helper view methods for). Just be sure to investigate what if any support they provide for XSS prevention.

Cramer answered 29/6, 2010 at 4:36 Comment(1)
As of Rails 3, you'll have to add .html_safe: <%= h(obj.description).gsub(/\n/, '<br/>').html_safe %>Stilbestrol
C
34

Keep user input unmodified and add this to your css:

white-space: pre-line;

It will display \r or \n (enter) in user input as a new line.

Copepod answered 11/2, 2015 at 2:30 Comment(4)
So much easier. Good answer.Thormora
This should be the answer.Enlargement
simple_format seems to only preserve 1 line break even though you have multiple when using in Mailer views. This answer works1Lemuel
Wow, can't believe this was here all these years. And it's supported by every browser!Octet
V
10

Here is another way to display the line breaks in a string while still escaping the rest of the text:

<%= safe_join(@object.textarea_input.split("\r\n"), "<br />".html_safe) %>
Versicular answered 19/11, 2012 at 23:3 Comment(2)
I liked and added my own customised thing in itWafer
Also you can use the tag helper, like this: safe_join(@object.textarea_input.split("\r\n"), tag(:br))Summersault
S
1

What version of rails are you using?? Because the way to handle this, is different in rails 2 and 3.

Let's say the value of the record is "foo<br />bar"

In rails 3, if you want to evaluate the html, you could do <%=raw "foo<br />bar" %>, if you do so, you'll get a line break when you will see the view.

In rails 2 you don't have to do that, just do <%= "foo<br />bar" %>

Also, HTML doesn't get evaluated in a textarea anyway.

Saddlebow answered 29/6, 2010 at 9:52 Comment(0)
E
1

See here http://code.byteblues.com/2012/03/23/preloading-a-text-input-area-text_area-with-data-that-contains-a-line-break/

<%=raw text_area_tag :keywords, keywords, :rows => 8 %>
Eparch answered 22/3, 2012 at 23:22 Comment(0)
S
1

the problem with simple_format is that it's also adding other tags like <b><i><hr><h1>...
if you just want line breaks without other tags i suggest you build a partial (lets call it line_break):

<% text.split("\n").each do |t| %>
  <%= t %><br>
<% end %>

then, just call it from your view:

<%= render partial: 'line_break', locals: {text: some_text} %>
Sensualist answered 27/11, 2012 at 12:2 Comment(0)
H
0

\n if memory serves (it hasn't been doing so well today... try at your own risk lol)

Edit: making the assumption you were talking about a textarea, if it is simple output, just use <br>

Homogenous answered 29/6, 2010 at 2:15 Comment(3)
Nah I tried \n as well as the same within bracket and it still just output the code as if it were textDaysidayspring
Could you provide the code you are using so we can get a better idea of what we are looking at instead of random guesses?Homogenous
Sure thing... the view has <p> <%= f.label :description %><br /> <%= f.text_area :description %> </p> thats pretty much it. I just wanted to turn off the html within the text areaDaysidayspring
L
0

The answers above were good:

  • the gsub (@Ian) worked well
  • the simple_format (@Karl) was a bit over the top as @Aaron pointed out, wrapping everything in <p>

So I tweaked as follows:

simple_format(value, {}, wrapper_tag: 'div')
Larisalarissa answered 19/7, 2014 at 20:10 Comment(0)
P
0

The other answers are wrong. Text area does not render
as line breaks, because the innerHTML value of the TEXTAREA element does not render HTML..

You need to add the Line feed HTML entity: &#10;

EXAMPLE:

<textarea><%= "LINE 1&#10;LINE 2&#10;LINE 3".html_safe %></textarea>

See also New line in text area - Stack Overflow

Pagandom answered 18/4, 2019 at 20:22 Comment(0)
C
-2

If you are simply displaying your string in the view. then try it with

< p >This is my text< / p >< br />
Codon answered 29/6, 2010 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.