Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails render helper to return a p tag with '\n' as new line

I am getting this string with the '\n' inside and I would like to display a new line where is has the '\n' rather than displaying the raw strings

inside the fruits.description

fruits.description: 'This is an red apple \n It is also very sweet'

my render helper method

def get_description(fruits)
  if fruits.type == 'apple'
    haml_tag 'p', fruits.description, {id: fruits.id}
  end
end

I want the strings to display so that is like this

This is an red apple
It is also very sweet
like image 562
AirWick219 Avatar asked Oct 12 '25 14:10

AirWick219


1 Answers

Try simple_format, which will add the <p> tag as well as translate \n to <br> etc.

Specifically:

Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended.

like image 101
meagar Avatar answered Oct 14 '25 11:10

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!