Jaqx
Jaqx

Reputation: 826

line break for a string placeholder in ruby

<%= f.text_area :comment, placeholder: "Comment on your track or" + \n + "share your favorite lyrics" %>

How can I get the placebolder to line break like

Solution was just to add white space so the next line wraps:

placeholder: "Comment on your track or                         share your favorite lyrics" %>

Pretty ugly but least complicated

Upvotes: 9

Views: 26586

Answers (3)

Tilo
Tilo

Reputation: 33752

In Ruby, in general "\n" is the new line character.

e.g.:

  puts "first line\nsecond line"
  => 
  first line
  second line

However, in your case:

you seem to try to use the newline character in an .erb expression <%= ... %>

That does not work, because that will only format the newline in the raw HTML souce, but in the formatted HTML you will not see the newline! :-)

To see the newline in the formatted HTML, you need to do something like this:

  • either put the two strings in separate DIVs or SPANs
  • or put a <br /> in the string instead of the "\n" -- <br \> is the HTML newline symbol

Upvotes: 6

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10218

The newline character \n should be included between the double, however HTML does not allow for line feed, but Thomas Hunter suggested an hack which consists in using a bunch of white spaces, like so:

<%= f.text_area :comment, placeholder: "Comment on your track or                  share your favorite lyrics" %>

You can also opt to use the title attribute instead.

Upvotes: 7

Aaron K
Aaron K

Reputation: 6971

You're creating HTML code. HTML does not care about whitespace in the actual code. What you need is a break in the HTML itself. However, it seems per this other question Can you have multiline HTML5 placeholder text in a <textarea>? that HTML does not allow line breaks in the placeholder field.

Upvotes: 1

Related Questions