Search code examples
ruby-on-railsformsline-breaks

linebreak from text_area missing in the database


I use a text_area in my test rails app

= form_for @post do |f|
  = f.text_field :title, size: '40'
  = f.text_area :description, size: "80x20"

and render markdown using RedCarpet

.post-description
  = markdown(post.description)

When I insert a list into the text_area, markdown is not rendered correctly due to missing line breaks in the description field.

The result is something like this

=markdown("1. test 2. test")

I guess linebreaks from the form need to be saved in the database. A simple string with a linebreak

=markdown("1. test /n 2. test")

worked as expected.

Can anyone give me a hint?


Solution

  • normally the textarea field have newline saved as \r\n, and it can be parse by the redcarpet. you need to check the post.description output, check if there exist some jquery plugin for the description edit, if there are callback which modify the description.Find the reason why description field it didn't have newline.

    try

    require 'redcarpet'
    
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new)
    
    p markdown.render("1. test 2. test")
    p markdown.render("1. eee\r\n2. 113")