Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails -- Add a line break into a text area

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?

like image 610
ChrisWesAllen Avatar asked Jun 29 '10 02:06

ChrisWesAllen


People also ask

How do you break a line through text area?

To add line breaks to a textarea, use the addition (+) operator and add the \r\n string at the place where you want to add a line break, e.g. 'line one' + '\r\n' + 'line two' . The combination of the \r and \n characters is used as a newline character.

How do I add a new line in rails?

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

How do you do a line break in Ruby?

\r\n should probably do the trick.

How do you code a line break?

The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.


2 Answers

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

like image 143
Karl Avatar answered Oct 16 '22 02:10

Karl


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.

like image 23
Ian Lesperance Avatar answered Oct 16 '22 03:10

Ian Lesperance