Dan Solo
Dan Solo

Reputation: 21

formatting from text area in rails app

I have a rails app that has a text input box for users to add a description of their work. I need it to maintain paragraphs however it just delivers everything on one line...

eg:This is the way it is inputted by the user in the text area...

This is my awesome movie that I have been working on for 12 years. It has been a labor of love.

This is how it ends up once the user submits the form...

This is my awesome movie that I have been working on for 12 years. It has been a labor of love.

I think this is the relevant part of code -

.field .name= f.label :description .value= f.text_area :description, :rows => 5,

The code is in a .haml file.

Any help would be greatly appreciated. Cheers

Upvotes: 0

Views: 3074

Answers (3)

Vik
Vik

Reputation: 5961

Try simple_format:

my_text = "Here is some basic text...\n...with a line break."

simple_format(my_text)

Upvotes: 9

Michael Slade
Michael Slade

Reputation: 13877

The line breaks are still there, just not being converted to HTML line breaks. To see this, view the source of the page with the content in it. You will see the line breaks there.

These line breaks need to be converted to html line breaks ie <br>.

To fix this, in your view template, change your reference to the content in from text to text.gsub("\n","<br>\n").

Upvotes: 0

Amar
Amar

Reputation: 6942

While displaying you can use Redcarpet markdown.

def markdown(content)

  markdown =Redcarpet::Markdown.new(Redcarpet::Render::XHTML,:hard_wrap=>true,:filter_html=>true,:autolink=>true,:no_intra_emphasis=>true)
  markdown.render(content).html_safe

end

and on UI

markdown(@movie.description)

Upvotes: 1

Related Questions