link_to image_tag 与 Rails 中的内部文本或 html

发布于 2024-10-24 05:20:41 字数 218 浏览 2 评论 0原文

我想使用 Ruby on Rails link_toimage_tag 方法输出以下内容:

<a href="#">Lorem Ipsum <img src="/images/menu-arrow-down.gif"></a>

Rails 中的好方法是什么?

I want to output the following with Ruby on Rails link_to and image_tag methods:

<a href="#">Lorem Ipsum <img src="/images/menu-arrow-down.gif"></a>

What would be a good way in Rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

孤独陪着我 2024-10-31 05:20:41

您可以使用块作为字符串插值的替代方案,并正确使用html_safe。例如:

<%= link_to '#' do %>
  Lorem Ipsum <%= image_tag('/images/menu-arrow-down.gif') %>
<% end %>

You can use blocks as an alternative to the string interpolation with correct usage html_safe. For example:

<%= link_to '#' do %>
  Lorem Ipsum <%= image_tag('/images/menu-arrow-down.gif') %>
<% end %>
贪了杯 2024-10-31 05:20:41

或者更短的方法是

<%= link_to image_tag('image/someimage.png') + "Some text", some_path %>

Or a shorter way is

<%= link_to image_tag('image/someimage.png') + "Some text", some_path %>
桃扇骨 2024-10-31 05:20:41

为什么不直接使用链接和图像标签呢?

<%= link_to "hiii #{image_tag(yourimagepath)}", "link_path" %>

您也可以尝试上面完成的附加操作(string + image_tag),但如果其中之一变为nil,则会抛出错误。使用插值,如果nil,它只会显示一个空白图像(或字符串)。

对于 Rails 3,您需要使用 html_safe

<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>

Why not just use a link to and image tag?

<%= link_to "hiii #{image_tag(yourimagepath)}", "link_path" %>

You could also try the appending done above (string + image_tag), but it will throw an error if one of those things becomes nil. Using interpolation, it will just show a blank image(or string) if it is nil.

For Rails 3, you will need to use html_safe:

<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>
同展鸳鸯锦 2024-10-31 05:20:41
link_to(image_tag("image.png", :alt =>  "alt text", :class =>"anyclass"), image_url) 
link_to(image_tag("image.png", :alt =>  "alt text", :class =>"anyclass"), image_url) 
他是夢罘是命 2024-10-31 05:20:41

我似乎不知道如何向 @corroded 的答案添加评论。但是,如果您使用的是 Rails 3,他的答案将无法按预期工作。如何解决这个问题可能不是很明显,至少对我来说不是。我知道我需要添加 html_safe,但把它放在了错误的位置。回想起来,这是显而易见的,但我想我应该把它放在这里,以供其他像我这样犯“菜鸟”错误的人使用。

<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>

但要小心,如果您为“hiii”使用用户可分配变量,则需要先对其进行清理。

<%= link_to (h(my_model.some_string) + image_tag(yourimagepath)).html_safe,
            "link_path" %>

请注意,my_model.some_string 或 yourimagepath 为 nil 没有问题,因为 h() 和 image_tag() 在传递 nil 时都会返回字符串。

I cannot seem to figure out how to add a comment to @corroded's answer. However, if you are using Rails 3, his answer will not work as expected. It might not be immediately obvious how to fix this, at least it wasn't for me. I knew I needed to add html_safe, but put it in the wrong place. In retrospect, it was obvious, but I thought I would include it here for others like me who make the "rookie" mistake.

<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>

be careful though, if you are using a user assignable variable for "hiii" you need to sanitize it first.

<%= link_to (h(my_model.some_string) + image_tag(yourimagepath)).html_safe,
            "link_path" %>

Note that there is no issue with my_model.some_string or yourimagepath being nil because both h() and image_tag() return strings when passed nil.

楠木可依 2024-10-31 05:20:41

我更喜欢 HAML 中的这种方法

= link_to root_path do
  = image_tag "ic_launcher.png", size: "16x16"
  Home

I prefer this approach in HAML

= link_to root_path do
  = image_tag "ic_launcher.png", size: "16x16"
  Home
§普罗旺斯的薰衣草 2024-10-31 05:20:41

这是使用 link_to 包含图像和带有文本的 mailto href(显示在图像旁边)的一种很酷的方法:

<%= link_to image_tag("image.png") + " [email protected]", 
                         href: "mailto:[email protected]" %>

在视图中提供类似的内容(整个图像和文本成为 mailto href):

带有mailto的超链接图像和文本

Here's a cool way to use a link_to to include an image, and a mailto href with Text (displayed next to the image):

<%= link_to image_tag("image.png") + " [email protected]", 
                         href: "mailto:[email protected]" %>

Giving you something like this in your view (the entire image and text become an mailto href):

hyperlinked image and text with mailto

感情洁癖 2024-10-31 05:20:41

对我来说这工作得很好:

<%= link_to(image_tag(<URL>,style),<URL>) %>

For me this worked just fine:

<%= link_to(image_tag(<URL>,style),<URL>) %>
梦晓ヶ微光ヅ倾城 2024-10-31 05:20:41

我发现这也有效。

link_to(image_tag("/images/linkd.png",:alt=>"twt"){},:href=>"some_url",:target=>"_blank")

i found out this also which worked.

link_to(image_tag("/images/linkd.png",:alt=>"twt"){},:href=>"some_url",:target=>"_blank")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文