views:

68

answers:

5

Hi, 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?

A: 

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

mynameiscoffey
Nah I tried \n as well as the same within bracket and it still just output the code as if it were text
ChrisWesAllen
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?
mynameiscoffey
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 area
ChrisWesAllen
A: 

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.

Ian
A: 

Hi ChrisWesAllen

If you are simply displaying your string in the view. then try it with < br />

Ex: < p >This is my text< / p >< br />

cheers

sameera

sameera207
A: 

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

Let's say the value of the record is "foo
bar"

In rails 3, if you want to evaluate the html, you would have to do <%=raw "foo
bar" %> and you would get a line break when seeing it in the view.

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

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

kentor
A: 

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

Karl