Konstantin Rudy
Konstantin Rudy

Reputation: 2257

How to render array of lines to <textarea>?

I'm on Ruby 2.0 and Rails 4, and trying to render an array of lines to f.text_area form helper using:

<%= f.text_area :sources_text, value: ['1', '2'].join('\n') %>

I expect to get:

1
2

as the <textarea> value but I get:

1\n2

What I'm doing wrong?

Upvotes: 1

Views: 970

Answers (2)

Uri Mikhli
Uri Mikhli

Reputation: 675

In the helper the value is being rendered as a string.

So to have

1
2

you have to have the value be "1\n2"

so if you have an array t then:

  <%t=['1','2']%>
  <%= f.text_area :sources_text, value: t.join("\n") %>

and you will have in the text area

1
2

Upvotes: 5

Phlip
Phlip

Reputation: 5343

Use: "\n". The '\n' version used single quotes '', which escape almost nothing.

Dev tip: Always prefer '' unless you actually need ""'s special powers (which you do need, here).

Upvotes: 5

Related Questions