Reputation: 12663
In a Rails app I have helper methods that render html snippets, e.g. Twitter bootstrap fonts
def edit_icon
content_tag(:i, "", :class=>'icon-edit')
end
I want to display this in a link anchor with additional text appended. e.g.
<%= link_to "#{edit_icon} Edit this Record", edit_record_path(@record) %>
This is currently rendering the content_tag as a string, not as HTML. How do I render it as HTML?
I experimented with <%= link_to "#{raw edit_icon}
and <%= link_to "#{edit_icon.html_safe}
, but these don't seem to be what I need in this case.
Thanks for any ideas.
Upvotes: 0
Views: 2965
Reputation: 624
The issue is Rails string interpolation transforms the HTML output of content_tag into a "safe" format. The fixes you tried both operate before string interpolation is applied, which won't work
Fixing the problem requires just a small change: move the method call outside of the string.
Do this:
<%= link_to edit_icon + "Edit this Record", edit_record_path(@record) %>
Instead of:
<%= link_to "#{edit_icon} Edit this Record", edit_record_path(@record) %>
Upvotes: 5