I recently had a problem returning the value returned by the yield in the template.
In my layout, I give a meta description so that I can define it from my template
<meta name="description" content="<%= yield :html_description %>" />
And here is my template, which unfortunately does not escape the value as expected:
<% content_for :html_description, 'hello "you" guy' %> <meta name="description" content="hello "you" guy" />
I tried to avoid this with h () escaper, but it does not work:
<meta name="description" content="<%= h(yield :html_description) %>" /> <meta name="description" content="hello "you" guy" />
I also tried with escape_once (), but it is too much:
<meta name="description" content="<%= escape_once(yield :html_description) %>" /> <meta name="description" content="hello &quot;you&quot; guy" />
However, concatenating the return value with a string, it fixes the problem:
<meta name="description" content="<%= '' + (yield :html_description) %>" /> <meta name="description" content="hello "you" guy" />
Does anyone understand this behavior?
Do you have a better solution than this concatenation that captures this by coincidence?
I am using Rails 2.3.8 - Thank you!
yield html ruby-on-rails escaping
Guillaume
source share