How to render html from a text column field in database?

1.1k views Asked by At

I have an column in database as welcome_emil of type TEXT.

I have saved the following code content saved in column as :

Thanks for Joining in!     \r\n\r\nYou can loginor refer your friends using your %referral_link%."

When i try to render this code into my email that is sent to the user. Complete content goes fine but

  \r\n\r\n

this is omitted and all text appear inline.

Here is the code that i use

  <%= p  @company.welcome_email.gsub('%referral_link%',@recipient.referral_link).html_safe %> 

Here is the screenshot how it looks like in email

enter image description here

3

There are 3 answers

0
Sahil Grover On BEST ANSWER

I used GEM REDCARPET. Details here http://rubygems.org/gems/redcarpet.

And the following code works perfectly fine for me.

<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true) %>
<% welcome_email = @site.welcome_email.gsub('% referral_link%',@recipient. referral_link) %>
<%= markdown.render(welcome_email).html_safe %>    

Thanks.

1
pjam On

You can use rails's method simple_format which will convert line ending into <br> tags

0
Aayush Khandelwal On

try this

     <%= @site.welcome_email.gsub(/\n/, '<br/>').html_safe %>