Rails - add line break to text area

I have a rails application where I can enter a few paragraphs of text into my model. The problem is that I do not know how to enter any line breaks.

I tried adding "{ln} {/ ln}; {& nbsp} and {br} {/ br}", but this only displays html as text and does not interrupt.

Is there anyway I can set it so that the text area control uses any of the html-i-places in the model record?

Is there any thing that I can print so that the rails know hey put a line here?

+58
html ruby-on-rails textarea
Jun 29 2018-10-06T00:
source share
11 answers

The problem is not so much in editing the value as in rendering it later. To add newlines to your value when editing in a text box, simply press the return key. When you re-edit this value later, spaces should still be there.

Rendering a space is the hard part. In HTML, spaces are usually not significant. A renderer similar to the one your browser uses displays one space for any continuous line of spaces. Therefore, simply resetting the value to the page will not be enough:

<%= obj.description %> 

Even if your value may be "One \t \n \n Two" , it will be displayed on the screen as "One Two" .

So that these new line characters are actually split into lines when displayed, you need to convert them to HTML before rendering:

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

Of course, if users enter data that will be included in your HTML, you should avoid values ​​to protect against XSS . If newlines are the only thing you need to maintain, it should be so simple:

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

If you want to allow more complex formatting, check out Markdown and Textiles (both of which Rails provide helper browsing methods). Just be sure to look into what if any support they provide to prevent XSS.

+83
Jun 29 2018-10-10T00:
source share

Line breaks in text areas are created as `\ n '. However, the problem is that if you just upload it to your view, it will just be a line break in your HTML source.

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

It automatically converts line breaks to HTML tags. You can use it with something like <%= simple_format(my_text_field) %> .

+104
Jun 30 2018-10-06T00:
source share

Do not enter user input unmodified and add it to your css:

 white-space: pre-line; 

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

+19
Feb 11 '15 at 2:30
source share

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

 <%= safe_join(@object.textarea_input.split("\r\n"), "<br />".html_safe) %> 
+8
Nov 19 '12 at 23:03
source share

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 entry is "foo<br />bar"

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

In rails 2 you do not need to do this, just <%= "foo<br />bar" %>

In addition, HTML is still not evaluated in the text field.

+1
Jun 29 '10 at 9:52
source share
+1
Mar 22 '12 at 23:22
source share

the problem with simple_format is that it also adds other tags like <b><i><hr><h1> ...
if you just want line breaks without other tags, I suggest you create a partial one (let's 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} %> 
+1
Nov 27
source share

\n if memory serves (today it is not so good ... try at your own risk lol)

Edit: by making the assumption that you were talking about textarea , if this is a simple conclusion, just use <br>

0
Jun 29 '10 at 2:15
source share

The answers above were good:

  • gsub (@Ian) worked well
  • simple_format (@Karl) turned upside down a bit, as @Aaron noticed, wrapping everything in <p>

So, I did the following:

 simple_format(value, {}, wrapper_tag: 'div') 
0
Jul 19 '14 at 20:10
source share

Other answers are incorrect. Text area not displayed
line breaks because the innerHTML value of the TEXTAREA element does not display HTML.

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

EXAMPLE:

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

See also Newline in text area - Stack Overflow

0
Apr 18 '19 at 20:22
source share

If you just show your line in the view. then try using

 < p >This is my text< / p >< br /> 
-one
Jun 29 '10 at 7:50
source share



All Articles