Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line breaks not displaying in view

Tags:

I have Person.description with the following stored in the database:

jnkl

fdsfdsf

fdsf

fsdfdsfs fds fd sf sdf ds

How do I display this with the line-breaks in the view? It is currently displaying all on one line and I don't understand why.

like image 567
Coderama Avatar asked Feb 27 '11 05:02

Coderama


People also ask

How do I enable line breaks?

When you add a line break in HTML, you avoid this text wrapping and start new text the next line. To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags.

Why does Outlook remove line breaks?

Microsoft Outlook removes line breaks in signatures for messages sent in plain text format. For the sender, the message looks normal but when displayed to the recipient, line breaks are removed so the formatting of the signature is compromised.


1 Answers

you should use the simple_format helper:

<%= simple_format @person.description %>

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

Source

# File actionview/lib/action_view/helpers/text_helper.rb, line 301
def simple_format(text, html_options = {}, options = {})
  wrapper_tag = options.fetch(:wrapper_tag, :p)

  text = sanitize(text) if options.fetch(:sanitize, true)
  paragraphs = split_paragraphs(text)

  if paragraphs.empty?
    content_tag(wrapper_tag, nil, html_options)
  else
    paragraphs.map! { |paragraph|
      content_tag(wrapper_tag, raw(paragraph), html_options)
    }.join("\n\n").html_safe
  end
end
like image 107
Abram Avatar answered Sep 27 '22 00:09

Abram