Tom Ha
2009-Feb-24 23:40 UTC
Displaying line breaks & paragraphs using ''simple_format'' ?
Hi there, I need to: 1. STORE a text of the below format in a (MySQL) database (using a ''textarea''), and then 2. RETRIEVE & DISPLAY it in exactly the same format (meaning: keeping all paragraphs/line breaks). The intended text format looks like this: *** Blah blahblah blah blah blahblah blah blah blahblah blah blah blahblah blah, blah blahblah. <- NEW PARAGRAPH Blah blahblah blah blah. <- LINE BREAK Blah blahblah blah blah blahblah blah blah blahblah blah blah blahblah blah, blah blahblah. *** Until now: - the text is stored in exactly this format in a ''text'' field in the database (which I believe is the correct way to do it) - I have used ''simple_format'' (http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001719) for displaying The problem is: LINE BREAKS are displayed correctly, but NOT the PARAGRAPHS (they appear like simple line breaks). What am I doing wrong ?! Thanks a bunch for any hints and help ! Tom -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gavin
2009-Feb-25 17:49 UTC
Re: Displaying line breaks & paragraphs using ''simple_format'' ?
Hey Tom-
sadly I noticed this same issue with simple_format
I get around it by adding a model method to whatever the class is with
the text.
For example, if I have pages, and each page has a body, I''ll add a
method like so:
class Page
def format_breaks
body.gsub("\n", "<br />")
end
end
and then call @page.body.format_breaks in the view.
This might lead to performance issues if you have an awful lot of text
though so it might be more efficient to create an after_save callback
instead.
Something like:
class Page
after_save :format_breaks
def format_breaks
update_attribute :body, body.gsub("\n", "<br />")
end
end
This will update the entry in the database after you save so all you
have to call in the view is <%= @page.body %>
Note - to keep your XHTML valid, you''ll need to wrap the whole thing
in paragraph tags (I believe?)
<p>
<%= @page.body %>
</p>
Hope that helps?
On Feb 24, 11:40 pm, Tom Ha
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hi there,
>
> I need to:
>
> 1. STORE a text of the below format in a (MySQL) database (using a
> ''textarea''), and then
> 2. RETRIEVE & DISPLAY it in exactly the same format (meaning: keeping
> all paragraphs/line breaks).
>
> The intended text format looks like this:
>
> ***
> Blah blahblah blah blah blahblah blah blah
> blahblah blah blah blahblah blah, blah
> blahblah. <- NEW PARAGRAPH
>
> Blah blahblah blah blah. <- LINE BREAK
> Blah blahblah blah blah blahblah blah blah
> blahblah blah blah blahblah blah, blah
> blahblah.
> ***
>
> Until now:
> - the text is stored in exactly this format in a ''text''
field in the
> database (which I believe is the correct way to do it)
> - I have used ''simple_format''
> (http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html...)
> for displaying
>
> The problem is: LINE BREAKS are displayed correctly, but NOT the
> PARAGRAPHS (they appear like simple line breaks).
>
> What am I doing wrong ?!
>
> Thanks a bunch for any hints and help !
> Tom
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Tom Ha
2009-Feb-25 19:18 UTC
Re: Displaying line breaks & paragraphs using ''simple_format'' ?
Thanks, Gavin! That definitely helps... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---