user984621
user984621

Reputation: 48453

Rails 4: How to execute HTML tags in text area?

I am trying to do something like this:

<% msg_textarea = "text text<br /> text" %>
<%= text_area_tag "my_texta", msg_textarea %>

And this is what is in the textarea:

text text<br /> text2

but I want:

text text
text2

I've tried:

<%= text_area_tag "my_texta", msg_textarea.html_safe %>
<%= text_area_tag "my_texta", simple_format(msg_textarea) %>
<%= text_area_tag "my_texta", simple_format(h msg_textarea) %>

But none of these worked for me. How to exectute HTML tags in textareas?

Thanks.

Upvotes: 0

Views: 512

Answers (2)

crazyjin
crazyjin

Reputation: 91

If br is the only tag troules you, gsub would help.
text = "text text
text2" text =text.gsub! "
", "\n"

Upvotes: 0

joshua.paling
joshua.paling

Reputation: 13952

You just need to use \n rather than <br />:

<% msg_textarea = "text text\n text" %>
<%= text_area_tag "my_texta", msg_textarea %>

Or alternatively, if you must have the <br /> in the original, something like:

<% msg_textarea = "text text<br /> text" %>
<%= text_area_tag "my_texta", msg_textarea.gsub('<br />', "\n") %>

Both will leave you with a space on the second line - but it's easy enough to replace that too if you wish.

Upvotes: 2

Related Questions