在Rails中的link_to正文中嵌入HTML

Rya*_*yan 68 ruby-on-rails link-to

在使用link_to方法生成的链接主体中获取嵌入式HTML的最佳方法是什么?

我基本上想要以下内容:

<a href="##">This is a <strong>link</strong></a>
Run Code Online (Sandbox Code Playgroud)

我一直试图按照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
Run Code Online (Sandbox Code Playgroud)

item_view.html.erb

 #...
 <%=raw picture_filter %>
 #...
Run Code Online (Sandbox Code Playgroud)

Val*_*dis 111

试试这种方式

<%= link_to(raw("a <strong>strong</strong> link"),{:pics => true},{ :class => 'highlight'})  %>
Run Code Online (Sandbox Code Playgroud)


mar*_*ark 53

= link_to "http://www.example.com" do
   <strong>strong</strong>
Run Code Online (Sandbox Code Playgroud)

  • 好吧,我只是:`<%= link_to destroy_user_session_path,方法:: delete do%>` (2认同)

Gor*_*Ape 27

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

<%= link_to url: my_path do %>
    This is a <strong>ape</strong>
<% end %>
Run Code Online (Sandbox Code Playgroud)

  • 我认为它应该是`<%= link_to my_path do%>这是<strong> ape </ strong> <%end%> (4认同)

Sye*_*yed 16

您可以使用 html_safe

<%= link_to ("<i class='someIcon'></i> Link").html_safe %>
Run Code Online (Sandbox Code Playgroud)