Ruby-on-rails 在 Rails 的 link_to 正文中嵌入 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5317951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:40:33  来源:igfitidea点击:

Embedded HTML in link_to body in Rails

ruby-on-railslink-to

提问by Ryan

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> tagbut with no luck. My code looks like the following:

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

item_helper.rb

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

item_view.html.erb

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

回答by Valdis

Try it this way

试试这个方法

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

回答by mark

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

回答by GorillaApe

As of 2016 I prefer this method.

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

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

回答by Syed

you can use html_safe

您可以使用 html_safe

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

回答by Burningpony

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})