Rails - 带 image_tag + 文本的 link_to

问题描述 投票:0回答:5
<%= link_to ((image_tag 'image.png'), 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>

这部分代码将生成我 image.png 作为链接。我需要在这个图像上附加一些文本(图像+文本),我尝试了类似的东西:

<%= link_to ((image_tag 'image.png', 'text'), 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>

还有类似的方法,但每次尝试都以有关语法错误的错误消息结束...有人可以帮助我吗,我应该如何正确设置它?

ruby-on-rails image link-to
5个回答
49
投票

试试这个。

<%= link_to image_tag('/images/image.png') + "some extra text", url_for({:controller => 'controller_name', :action => 'action_name'}), :class => 'quick', :remote => true %>

12
投票

一个稍微性感的解决方案?

<%= link_to image_tag("image.png", :alt => "Image Description", :class => "css"), root_path %>

1
投票

试试这个:

<%= link_to (image_tag('image.png') + text, 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>

第一个参数是文本部分,使用 image_tag 创建 HTML,但您可以轻松附加内容。


0
投票

我使用了以下内容,效果很好:

<%= link_to image_tag("logo.jpg"), controller: 'welcome' %>

0
投票

我知道已经太晚了,但也许有人试图找到答案,所以。 正确答案是:

<%= link_to image_tag(product.image_url), line_items_path(product_id: product), method: :post, remote: true %>

在这种情况下,我们需要指定我们要执行的具体操作,通过单击此图像,如果我们使用button_to helper,我们不需要指定操作,但button helper不支持图像标签

© www.soinside.com 2019 - 2024. All rights reserved.