73

在使用 link_to 方法生成的链接正文中嵌入 HTML 的最佳方法是什么?

我基本上想要以下内容:

<a href="##">This is a <strong>link</strong></a>

我一直在尝试按照Rails 和 <span> 标签中的建议进行此操作,但没有运气。我的代码如下所示:

item_helper.rb

def picture_filter
    #...Some other code up here
    text = "Show items with " + content_tag(:strong, 'pictures')
    link_to text, {:pics => true}, :class => 'highlight'
end

item_view.html.erb

 #...
 <%=raw picture_filter %>
 #...
4

5 回答 5

113

试试这个方法

<%= link_to(raw("a <strong>strong</strong> link"),{:pics => true},{ :class => 'highlight'})  %>
于 2011-03-15T21:12:07.100 回答
60
= link_to "http://www.example.com" do
   <strong>strong</strong>
于 2011-03-15T21:14:15.070 回答
30

截至 2016 年,我更喜欢这种方法。

<%= link_to my_path do %>
    This is a <strong>ape</strong>
<% end %>
于 2016-05-09T11:10:24.710 回答
17

您可以使用html_safe

<%= link_to ("<i class='someIcon'></i> Link").html_safe %>
于 2014-04-23T06:49:16.540 回答
4

不确定这是否是最好的方法。

但是我已经非常成功地在 content_tag 调用中放置了很多视图助手。

调用 .html_safe 也可能没有坏处

link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true})
于 2011-03-15T21:22:28.320 回答