Forum: Ruby on Rails Going nuts on '\n' to <br />

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.
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-17 22:47
(Received via mailing list)
Hi all,

Is there no way to easily resolve this. It can't be that I'm the first
that
wants this, and it wouldn't be RoR if it's not possible.

On the mysql prompt with the select description from todos .. linefeeds
are
show, so they are there.

This is the actual contents with a linefeed after the ":"

"Project contacts:
delete table, point project to contacts"

But this: <%= todo.description.tr('\n', '<br />') %>

Results in this:

Project co
(It actually replaces "n",and it's on to the next cell)

And with double quotes int the statement it's this:

"Project contacts:"

(and it's on to the next cell).

Tried RedCloth but that doesn't work as simple either (not at all
actually,
because it doesn't know textilize or textile as a methgod after install)

Help is greately appreciated.

Regards,

Gerard.


--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
E48d29dc8fedb2878fa518d41cc63d88?d=identicon&s=25 Jan Prill (Guest)
on 2006-01-17 22:59
(Received via mailing list)
Hi Gerard,

regarding RedCloth: sounds as if you didn't make rails to know about
RedCloth. Is

require 'rubygems'
require_gem 'RedCloth'

in your environment.rb?

whats .tr about (don't know this method). You might have a try with
todo.description.gsub('\n', '<br />')

Best Regards
Jan Prill
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-17 23:33
(Received via mailing list)
Jan,

Found it after chewing on your input. This one does the trick. gsub
should be
written with slashes: <%= todo.description.gsub(/\n/, '<br />') %><

What still confuses me though is that redcloth is not available:

> require 'rubygems'
> require_gem 'RedCloth'
I put these in and restarted webrick. WIth the result:

undefined method `textile' for "Project contacts:\ndelete table, point
project
to contacts":String
(textilize doesn't work either)

> whats .tr about (don't know this method). You might have a try with
> todo.description.gsub('\n', '<br />')
I've seen the "tr" come by somewhere in this list and is mentioned on
the ruby
book:

http://www.ruby-doc.org/docs/ProgrammingRuby/

Under:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/...

Thanx a lot!

Regards,

Gerard.

> > On the mysql prompt with the select description from todos .. linefeeds
> >
> > actually, because it doesn't know textilize or textile as a methgod after
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
5316d692cc603c3ad2df0f03869bb96b?d=identicon&s=25 Mik Mifflin (Guest)
on 2006-01-18 00:39
(Received via mailing list)
String#tr is for translating single characters to other single
characters.  You can't replace a single character with more than one
characer using tr.  Use String#gsub instead.

todo.description.gsub( "\n", "<br />" )

On 1/17/06, Gerard Petersen <mailing@gp-net.nl> wrote:
> "Project contacts:
>
>
>
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
 - Mik Mifflin
"Whether freedom is going to survive at all is in doubt, but we've got
to try" - RMS
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2006-01-18 00:51
(Received via mailing list)
On Tue, Jan 17, 2006 at 10:10:28PM +0100, Gerard Petersen wrote:
> (It actually replaces "n",and it's on to the next cell)
(Taking into account your later e-mails as well)

The problem isn't that you need to feed gsub a regex (which is what /\n/
actually gives you) but rather than single quotes don't expand escape
sequences like \n.  What you really needed was gsub("\n", '<br />')
instead,
which gives you the correct result.

- Matt
3cffdac8c6712eeec41d2e35abee90b9?d=identicon&s=25 Jonathan Dance (Guest)
on 2006-01-18 01:06
(Received via mailing list)
I figured I would just add that you may want to use the
"simple_format" function - it doesn't just do \n to <br /> but a few
other things and wraps in a <p> tag - not always desirable but since
you were considering using Textile I figured it was worth mentioning.

Docs on simple_format are here:
http://api.rubyonrails.com/classes/ActionView/Help...

If you take a look at the source it shows you'll see it's some regexs,
nothing fancy, but nevertheless useful.
895a112c66d93cefcd06d2cf371219e6?d=identicon&s=25 Hasan Diwan (Guest)
on 2006-01-18 19:34
(Received via mailing list)
Gerard:
On Jan 17, 2006, at 1:10 PM, Gerard Petersen wrote:
>
> But this: <%= todo.description.tr('\n', '<br />') %>

<%= todo.description.gsub '\n','<br />' %> works on your example string.
Cheers,
Hasan Diwan <hasan.diwan@gmail.com>
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-19 01:24
(Received via mailing list)
Hmm .. so '\n' would be expanded before used, that right?

GrtzG

On Wednesday 18 January 2006 00:11, Matthew Palmer tried to type
something
like:
> > Project co
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-19 01:27
(Received via mailing list)
Looks nice TNX!

Although I got it working with gsub I'm still wondering why RedCloth is
nagging me with unknown method errors .. :-(

GrtzG

On Wednesday 18 January 2006 00:27, Jonathan Dance tried to type
something
like:
> nothing fancy, but nevertheless useful.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-01-19 01:33
(Received via mailing list)
'\n' is a two character string '\n'[0] => \   and   '\n'[1]  =>  n
"\n" is a one character string

get your ascii chart and fire up irb!

irb(main):001:0> '\n'[0]
=> 92
irb(main):002:0> '\n'[1]
=> 110
irb(main):003:0> "\n"[0]
=> 10
irb(main):004:0> "\n"[1]
=> nil
irb(main):005:0>


single-quoted strings don't interpolate their contents, not even for
\-escapes

-Rob
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-19 01:36
(Received via mailing list)
That's weird,

it didn't when used in the template. any idea?

GrtzG

On Wednesday 18 January 2006 18:55, Hasan Diwan tried to type something
like:
> Gerard:
>
> On Jan 17, 2006, at 1:10 PM, Gerard Petersen wrote:
> > But this: <%= todo.description.tr('\n', '<br />') %>
>
> <%= todo.description.gsub '\n','<br />' %> works on your example string.
> Cheers,
> Hasan Diwan <hasan.diwan@gmail.com>

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-19 01:39
(Received via mailing list)
I'll hack on that thanks!

So those win cr/lf's would contain two chars then when in between
doubles?

GrtzG

On Thursday 19 January 2006 00:55, Rob Biedenharn tried to type
something
like:
> => 10
> At 1/18/2006 06:45 PM, Gerard Petersen wrote:
> > > > "Project contacts:
> > >
> >
> >Rails mailing list
> >Rails@lists.rubyonrails.org
> >http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matt Palmer (Guest)
on 2006-01-19 02:00
(Received via mailing list)
On Thu, Jan 19, 2006 at 12:45:57AM +0100, Gerard Petersen wrote:
> > > Results in this:
>
> Hmm .. so '\n' would be expanded before used, that right?

No, '\n' is a two character String object -- a literal backslash
followed by
a literal lowercase n.  "\n" is a single character String object,
containing
the newline character ASCII code 0x0A.  (On windows, "\n" *may* expand
to
the two-character String object 0x0D 0x0A under some circumstances, but
I've
got no experience with Ruby on windows, so if you're on windows you're
on
your own).

- Matt
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-01-19 02:12
(Received via mailing list)
At 1/18/2006 07:21 PM, Matt Palmer wrote:
> > > >
> > > instead, which gives you the correct result.
>- Matt
*nix or Windoze, doesn't seem to matter here:

$ uname
Linux
$ irb
irb(main):001:0> '\n'.length
=> 2
irb(main):002:0> "\n".length
=> 1
irb(main):003:0> "\n"[0]
=> 10
irb(main):004:0>


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>irb
irb(main):001:0> '\n'.length
=> 2
irb(main):002:0> "\n".length
=> 1
irb(main):003:0> "\n"[0]
=> 10
irb(main):004:0>

-Rob
4710a8ada2adc162aaee4288d9879d8e?d=identicon&s=25 Gerard Petersen (Guest)
on 2006-01-19 22:26
(Received via mailing list)
No windows for me thanx.

I was just curious.

TNX and Grtz

Gerard.

On Thursday 19 January 2006 01:21, Matt Palmer tried to type something
like:
> > > >
> > > /\n/ actually gives you) but rather than single quotes don't expand
> you're on windows you're on your own).
>
> - Matt
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
"Who cares if it doesn't do anything?  It was made with our new
Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."

My $Grtz =~ Gerard;
~
:wq!
This topic is locked and can not be replied to.