Topic: Adding a line break.. safely

I have a standard text area, the contents which gets saved to the db and then rendered again:

<%= text_area 'review', 'review_body', :cols => 50, :rows => 8  %>

I render it using sanitize and simple format:
<%= simple_format(sanitize(review.review_body)) %>

My issue is that it removes any extra line breaks so for instance if the user inputs

foo

bar

It comes out as

foo
bar

Is there any way to preserve the line breaks? Maybe a still safe alternative to simple_format?

Re: Adding a line break.. safely

1) Change review_body to just "body". It is an unneeded repetition of the word "review"

2) review.body.gsub("\n","<br />")

Re: Adding a line break.. safely

Perfect! Thanks!!

Re: Adding a line break.. safely

SO SO AWESOME! cheers!

Re: Adding a line break.. safely

Simple format returns two <p> tags for a double line space - which is semantically correct - you shouldn't be using <br /> to separate your content if your double line space is supposed to signify a new paragraph.

If you want to space your <p> tags out you will need to add margin using CSS.

Re: Adding a line break.. safely

Maybe you are wonder how to only accept certain tags and line breaks.
Here is my solution:

def format_content(content)
  # Allow only tags specified in tags options, likewise in attributes
  content = sanitize(content, :tags => %w(b strong i em img), :attributes => %w(src))
  # Add Support for Line Breajs
  content = simple_format(content)
  # Autolink Urls and Emails
  content = autolink(content, :all, :target => '_blank')
  # Return it, baby
  return content
end

Hopefully this will help some. If you have a a better way of doing it, please reply!
  content =