Hi, I am storing multi-lined information in a text field in the database, and intaking it using a text_area field. When i output it, the information is all streamed into one line. How can I output the information with the line breaks. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Replace ''\n'' with ''<br />'' (or ''<br />\n'') in your string when rendering it. Cheers. On 9/13/06, lundie <rlundie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi, > > I am storing multi-lined information in a text field in the database, > and intaking it using a text_area field. When i output it, the > information is all streamed into one line. How can I output the > information with the line breaks. > > Thanks! > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I define the following method in one of my helper files:
# wraps each line to width characters, splitting lines on a
whitespace character
def wrap_text(text, width = nil)
char_count = 0
0.upto(text.length) do |i|
point = i;
# reset the character counter if we encounter a newline
# before we''ve reached width characters
char_count = 0 if text[point..point] == "\n";
if char_count > width
# once we hit the desired width of characters, start
# moving backwards one character at a time until we
# encounter a whitespace character
point -=1 while text[point..point] != " "
text.insert(point+1, "\n")
# we''ve inserted a newline, reset the character counter
char_count = 0;
else
char_count+=1
end
end
return text
end
I''m sure there''s an easier method for line wrapping (if anyone
else
has a better method, I''d love to see it). I did come across the
following site earlier today, which includes a _much_ simpler method
for line wrapping, although unfortunately it doesn''t work:
http://redhanded.hobix.com/inspect/methodCheckDateSucc.html
the method is the following:
str = "Hello. Ruby rocks. " * 50
while space_pos = str.rindex(" ", 80)
str[space_pos, 1] = "\n"
end
but it fails to correctly line wrap, since each call to str.rindex
doesn''t take into account where the previous newline character was
inserted.. I just didn''t want to spend any time fixing it, since
I''ve
already got a working line wrap method (albeit mine is a bit
cumbersome).
Mike
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
You might want to use the simple_format TextHelper: simple_format(text) Returns text transformed into HTML using very simple formatting rules Surrounds paragraphs with <p> tags, and converts line breaks into <br/> Two consecutive newlines(\n\n) are considered as a paragraph, one newline (\n) is considered a linebreak, three or more consecutive newlines are turned into two newlines -- 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 -~----------~----~----~----~------~----~------~--~---
Mike Garey wrote:> I''m sure there''s an easier method for line wrapping (if anyone else > has a better method, I''d love to see it). I did come across the > following site earlier today, which includes a _much_ simpler method > for line wrapping, although unfortunately it doesn''t work:Mike, There''s an existing word_wrap TextHelper function that appears to do the same thing as yours. It''s implementation is also a wee bit smaller: def word_wrap(text, line_width = 80) text.gsub(/\n/, "\n\n").gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip end -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks Curtis, that''s good to know.. Although I think I''ll stick to my current method, as this one seems to remove lines with nothing but a line feed on them, which I often have, since I''m displaying the contents of user entered text in a <pre></pre> tag. With my method, I get pretty much exactly what the user has entered, including their empty linefeeds, but wrapped so that it doesn''t extend past the width of the page. Mike On 9/13/06, Curtis Summers <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Mike Garey wrote: > > I''m sure there''s an easier method for line wrapping (if anyone else > > has a better method, I''d love to see it). I did come across the > > following site earlier today, which includes a _much_ simpler method > > for line wrapping, although unfortunately it doesn''t work: > > Mike, > > There''s an existing word_wrap TextHelper function that appears to do the > same thing as yours. It''s implementation is also a wee bit smaller: > > def word_wrap(text, line_width = 80) > text.gsub(/\n/, "\n\n").gsub(/(.{1,#{line_width}})(\s+|$)/, > "\\1\n").strip > end > > > > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Thanks for all the help! I ended up using the simple_format helper as a
base, but modified it so that it didn''t put <p> tags around the
entire
thing.
def simple_format2(text)
text.gsub!(/(\r\n|\n|\r)/, "\n") # lets make them newlines
crossplatform
text.gsub!(/\n\n+/, "\n\n") # zap dupes
text.gsub!(/\n\n/, ''</p>\0<p>'') # turn two
newlines into paragraph
text.gsub!(/([^\n])(\n)([^\n])/, ''\1\2<br />\3'') #
turn single
newline into <br />
end
Thanks again!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---