i have one mailer file called schedule_mailer.html.erb in that I'm trying to use a helper method from that I'm rendering the contents
in scheudle_mailer.html.erb
<p style='font-size:15px;margin:0 25px 20px 25px' >
<%= body_text_tag() %>
</p>
in helper.rb
i need to send a string which includes <b>
as well as <br>
but I'm receiving in mail as <b>
and <br>
tag itself.
my helper method
def body_text_tag()
body = "This email to inform you that"
if @creation
body + "your hotel schedule has been created <br>
<b>timings :</b> #{@timings}, <br>
<b>Room number :</b> #{@room number},<br>
....
....
....
elsif ....
.....
end.
as I have tried tag(:br), inside the string, even though it's rendering the mail with the tag(:br) itself, instead of the break line. how to approach this kind of tags in the string to HTML?
The reason this doesn't work is that in Rails when you append a normal string to a safe buffer its automatically escaped to prevent XSS attacks.
irb(main):076:0> "<b>Hello".html_safe + "World</b>"
=> "<b>HelloWorld</b>"
If you want to create text that contains HTML tags from a helper you need to mark the portions that are HTML tags as safe:
"<b>".html_safe + @timings + "</b>".html_safe
Do not do:
"<b>#{ @timings }</b>".html_safe
Unless you actually know that the content is actually safe and doesn't contain user input.
This is what the tag helpers do automatically for you:
content_tag(:b, @timings)
tag.b(@timings)
But this code most likely doesn't actually belong in a helper in the first place. Use a view or a partial and write it in ERB.