Newline in string

Hello,

The following is a toggle div, in which I’m trying to add a newline
after the word ‘notes:’

<%= content_tag(:div, content_tag(:p, “Our programme notes are being
prepared.”, :class => ‘regtextend’), :id => ‘notes’) %>

And this doesn’t work:

<%= content_tag(:div, content_tag(:p, “Our programme notes\nare being
prepared.”, :class => ‘regtextend’), :id => ‘notes’) %>

When using simple_format, \n breaks the line. I see that it wouldn’t
work the same way, but is there a way to escape in the middle of a
string in this case? Help would be appreciated! Thanks – Jon.

Jon Snidal wrote in post #1009527:

The following is a toggle div, in which I’m trying to add a newline
after the word ‘notes:’

<%= content_tag(:div, content_tag(:p, “Our programme notes are being
prepared.”, :class => ‘regtextend’), :id => ‘notes’) %>

And this doesn’t work:

<%= content_tag(:div, content_tag(:p, “Our programme notes\nare being
prepared.”, :class => ‘regtextend’), :id => ‘notes’) %>

When using simple_format, \n breaks the line. I see that it wouldn’t
work the same way, but is there a way to escape in the middle of a
string in this case? Help would be appreciated! Thanks – Jon.

Remember that a newline character in HTML is just more whitespace like
any other whitespace. To see an actual new line on the rendered page you
either need to put the two lines in separate

tags or use a

tag to break the text to the next line.

Also using the content_tag helper inline in ERb doesn’t really make a
lot of sense either. You could just write the HTML. The content_tag
helper is useful inside of a view helper, but not so much inside of the
view template.

Why would it? is html what gets rendered, use
and end the string
with
html_safe

Try something like this

<%= content_tag(:div, content_tag(:p, “Our programme notes are being
prepared.
”, :class => ‘regtextend’), :id => ‘notes’).html_safe %>

Or
<%= raw content_tag(:div, content_tag(:p, “Our programme notes are being
prepared.
”, :class => ‘regtextend’), :id => ‘notes’).html_safe %>
On Fri, Jul 8, 2011 at 5:50 PM, radhames brito [email protected]
wrote:

http://groups.google.com/group/rubyonrails-talk?hl=en.


Rajeev Kannav
Sharmahttp://www.google.com/search?q=Rajeev+Kannav+Sharma&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Jon Snidal wrote in post #1009692:

I couldn’t actually get it to work, but good to learn about html_safe!
I ended up with:

<%= content_tag(:div, simple_format(“Our programme notes\nare being
prepared.”, :class => ‘regtextend’), :id => ‘notes’) %>

Not sure if that’s the best solution, but it works well. Thanks again
for the help – Jon.

Again. Why are you using content_tag helper at all?

This would render exactly what you show in your example:

Our programme notes
are being prepared.

You don’t even have any Ruby in the example you show above?

I couldn’t actually get it to work, but good to learn about html_safe!
I ended up with:

<%= content_tag(:div, simple_format(“Our programme notes\nare being
prepared.”, :class => ‘regtextend’), :id => ‘notes’) %>

Not sure if that’s the best solution, but it works well. Thanks again
for the help – Jon.