2

I have a text_area tag which allows the user to enter his Bio. When a user is tyoing and if he hits enter or return, a new line starts. But when he saves his input all the text is displayed in one paragraph. I want functionality similar to what stack overflow has.

For example - I hit enter now

This text appears on a new line*

How can I do this?

This is my code in Rails:

<%= form_for :profile do |profile| %>
    <%= profile.text_area :bio %>

<%= f.submit "Save Bio" %></p>
<% end %>
4

2 回答 2

7

You should use text editor for example ckeditor (to simplify web content creation), and in view try simpleformat or raw:

<%= simple_format("Here is some basic text...\n...with a line break.") %>
<%= raw("Here is some basic text...<br/>...with a line break.") %>
于 2013-09-09T06:32:07.950 回答
4

There are many ways to handle this. When displaying text previously inputed in text area you can:

  1. replace newline characters with <br/> tags
  2. use <pre> tag and display text inside that tag
  3. split text by newline characters and then wrap each of the chunks into <p> tags

When using approach 1 or 3, make sure to pass text through raw helper, so that any tags within text are displayed. Be aware though, that user may pass arbitrary html inside the textarea, hence your code may be subject to xss attacks.

于 2013-09-09T06:30:04.490 回答