Rails — Add a line break into a text area

这一生的挚爱 提交于 2019-11-28 15:12:33

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.

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

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.

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

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

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.

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

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')

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

sameera207

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

< p >This is my text< / p >< br />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!