在link_to主体中嵌入HTML在Rails中

时间:2022-11-12 12:47:52

What is the best way to go about getting embedded HTML in the body of a link generated with the link_to method?

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

I basically want the following:

我基本上想要的是:

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

I have been trying to go about this as suggested in Rails and the <span> tag but with no luck. My code looks like the following:

我一直在尝试按照Rails和标签中建议的那样去做,但是运气不好。我的代码如下所示:

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 %>
 #...

5 个解决方案

#1


97  

Try it this way

试试这种方法

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

#2


46  

= link_to "http://www.example.com" do
   <strong>strong</strong>

#3


17  

As of 2016 I prefer this method.

从2016年开始,我更喜欢这种方法。

<%= link_to url: my_path do %>
    This is a <strong>ape</strong>
<% end %>

#4


10  

you can use html_safe

您可以使用html_safe

<%= link_to ("<i class='someIcon'></i> Link").html_safe %>

#5


4  

Not sure if this is the best way.

不知道这是不是最好的方法。

But I have been very successful in staking alot of the view helpers inside the content_tag call.

但是我非常成功地将许多视图助手放在content_tag调用中。

It also might not hurt to call a .html_safe

调用.html_safe也无妨

link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true})

#1


97  

Try it this way

试试这种方法

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

#2


46  

= link_to "http://www.example.com" do
   <strong>strong</strong>

#3


17  

As of 2016 I prefer this method.

从2016年开始,我更喜欢这种方法。

<%= link_to url: my_path do %>
    This is a <strong>ape</strong>
<% end %>

#4


10  

you can use html_safe

您可以使用html_safe

<%= link_to ("<i class='someIcon'></i> Link").html_safe %>

#5


4  

Not sure if this is the best way.

不知道这是不是最好的方法。

But I have been very successful in staking alot of the view helpers inside the content_tag call.

但是我非常成功地将许多视图助手放在content_tag调用中。

It also might not hurt to call a .html_safe

调用.html_safe也无妨

link_to(content_tag(:span, "Show yada " + content_tag(:strong, "Pictures")), {:pics => true})