Forum: Ruby on Rails Textilize problems with line breaks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dylan M. (Guest)
on 2006-03-25 04:12
(Received via mailing list)
I'm having a problem formatting some text. I have a textarea that I
input my text into, which will then be put into an e-mail and sent out.

The problem I am having is when there is a single line break.
Multiple line breaks work fine (hitting enter twice), but single line
breaks don't seem to work properly.

Take the following input:

Line1.

Line2.

Line3.
Line4.

Using <%= textilize @body %> gives me:

Line1.
Line2.
Line3.
Line4.

Knowing that there's an issue in rails with hard_breaks and
textilize, I tried RedCloth.new(@body).to_html, which gave me:

Line1.

Line2.

Line3.Line4.

Am I missing something??
Dorian M. (Guest)
on 2006-03-25 20:31
(Received via mailing list)
try:

sometext = RedCloth.new(@body, :hard_breaks)
sometext.to_html

although, judging by http://redcloth.rubyforge.org/rdoc/ that should be
the default behaviour, and the rails 'textilize' method should also do
that by default:

    # File
vendor/rails/actionpack/lib/action_view/helpers/text_helper.rb, line 84
84:         def textilize(text)
85:           if text.blank?
86:             ""
87:           else
88:             textilized = RedCloth.new(text, [ :hard_breaks ])
89:             textilized.hard_breaks = true if
textilized.respond_to?("hard_breaks=")
90:             textilized.to_html
91:           end
92:         end

I've had loads of problems with various platforms and hard_breaks.
what is your actual html output from the various options you tried?
Dylan M. (Guest)
on 2006-03-25 21:55
(Received via mailing list)
Doing that gives me the exact same output that textilize(@body) does.

Textilize(@body) and RedCloth.new(@body, [:hard_breaks]).to_html both
give me:

<p>Test1.<br />Test2.<br />Test3.<br />Test4.</p>

RedCloth.new(@body).to_html (without hard_breaks) gives me:

<p>Test1.</p><p>Test2.</p><p>Test3. Test4.</p>

I've also tried doing a textilize(@body.gsub("\r\n","<br/>")) to
force one <br/> per line break, but then it screws up things like
bulleted and numbered lists.
This topic is locked and can not be replied to.