Satchel
Satchel

Reputation: 16724

How do I inject <br> into text entered through a textarea in rails?

I have a textarea form which takes a large block of text. In this text area, I do a carriage return to end the paragraph and another carriage return to separate the paragraphs.

That text is in @contact_postalcard.message.

However, I need to output into an HTML file. The HTML file has been loaded as a long string which contains 'ReplaceThisWithPostalcardMessage' in it. I want to gsub the text from @contact_postalcard.message for ReplaceThisWithPostalcardMessage.

The problem is that the HTML file does not have any
tags for each carriage return. As a result, I get one long run-on paragraph.

How can I format the substituted value in the HTML file properly?

addr_template = addr_template.gsub(/ReplaceThisWithPostalcardMessage/, @contact_postalcard.message)

Upvotes: 0

Views: 607

Answers (2)

David Lyod
David Lyod

Reputation: 1438

Use the textarea as normal then display using :

simple_format(data_from_textarea)

Upvotes: 4

ngoozeff
ngoozeff

Reputation: 4746

How about this?

addr_template = addr_template.gsub(/ReplaceThisWithPostalcardMessage/,
  @contact_postalcard.message.gsub("\n", "<br />"))

Upvotes: 0

Related Questions