4

I'm not sure why "\n\n" is not adding two line breaks in my code below:

<%= (getparagraph(@geography, "geography")+"\n\n") if @geography != "Other" %> 

To clarify the method getparagraphs simply returns a paragraph of text. I'm trying to add two line breaks within the Ruby code without having to use <br/>

Any ideas? Everything I've read implies that it should work.

rsp
  • 23,135
  • 6
  • 55
  • 69
Edward Castano
  • 617
  • 2
  • 8
  • 14

3 Answers3

4

You're outputting HTML; whitespace is largely ignored upon rendering.

Use <br/> tags instead, and .html_safe.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
4

Your helper has "paragraph" in its name so maybe you should wrap it in a paragraph and use CSS to add the appropriate spacing around the paragraph:

<% if @geography != "Other" %>
    <p>
      <%= getparagraph(@geography, "geography") %>
    </p>
<% end %>

You could always add a special class to the <p> if you need an extra spacing after this:

<% if @geography != "Other" %>
    <p class="geo-chunk">
      <%= getparagraph(@geography, "geography") %>
    </p>
<% end %>

and then in your CSS:

.geo-chunk {
    margin-bottom: 2em; /* Or whatever works */
}

And if this is to appear inside another <p> then you'd need to re-arrange the HTML a bit as you can't put a block element inside a <p>:

<div>
    <!-- The old <p> content ... -->
    <% if @geography != "Other" %>
        <div class="geo-chunk">
          <%= getparagraph(@geography, "geography") %>
        </div>
    <% end %>
</div>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Many thanks! I went with option 1. Of course, I didn't think to place the

    or
    tag between the if and end statements.

    – Edward Castano Nov 11 '11 at 21:31
  • @Edward: Cool, I had to tweak option 2 a bit BTW, I remember that you can't (legally) put any block elements inside a `

    ` but that doesn't have anything to do with option 1.

    – mu is too short Nov 11 '11 at 23:59
1

Is this within HTML? If so, then just use <br>. HTML ignores multiple whitespace characters, so you could have one space or 50 newlines and the result would be the same.

Another note: unless you're using XHTML (itself not a great idea), do not use self-closing tags such as <br/>. The slash is invalid, so omit it.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
  • Actually, strictly speaking, the slash is valid, but semantically different from what you might expect. See http://stackoverflow.com/questions/3201870/are-self-closing-input-tags-valid-in-html-4 . – Marnen Laibow-Koser Nov 11 '11 at 21:37