如何在link_to锚点中渲染html + string?

And*_*vey 0 ruby-on-rails

在Rails应用程序中,我有帮助方法来呈现html片段,例如Twitter引导字体

def edit_icon
  content_tag(:i, "", :class=>'icon-edit')
end
Run Code Online (Sandbox Code Playgroud)

我希望在附加了附加文本的链接锚中显示它.例如

<%= link_to "#{edit_icon} Edit this Record", edit_record_path(@record) %>
Run Code Online (Sandbox Code Playgroud)

这当前将content_tag呈现为字符串,而不是HTML.如何将其呈现为HTML?

我尝试用<%= link_to "#{raw edit_icon}<%= link_to "#{edit_icon.html_safe},但这些似乎并没有什么,我需要在这种情况下.

谢谢你的任何想法.

小智 5

问题是Rails字符串插值将content_tag的HTML输出转换为"安全"格式.您尝试的修复程序在应用字符串插值之前都会运行,这将无效

解决问题只需要进行一些小改动:将方法调用移到字符串之外.

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) %>
Run Code Online (Sandbox Code Playgroud)