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

This part of code will generate me image.png as a link. I would need to this image append some text (image + text), I tried something like a:

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

And similar ways, but each of these attempts ended with an error message about bad syntax... Could anyone help me, please, how I should set it right?

Thanks in advance.

user1946705
  • 2,858
  • 14
  • 41
  • 58

5 Answers5

49

Try this.

<%= link_to image_tag('/images/image.png') + "some extra text", url_for({:controller => 'controller_name', :action => 'action_name'}), :class => 'quick', :remote => true %>
Muhammad Sannan Khalid
  • 3,127
  • 1
  • 22
  • 36
12

A slightly sexier solution?

<%= link_to image_tag("image.png", :alt => "Image Description", :class => "css"), root_path %>
Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71
1

Try this:

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

The first argument is the text part and with image_tag you create HTML, but you can easily append stuff.

ayckoster
  • 6,707
  • 6
  • 32
  • 45
0

I used the following and it works just fine:

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

I know it`s too late, but maybe someone trying to find the answer, so. The right answer is:

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

In this case, we need to specify what exact action we want to perform, by clicking on this image, if we are using button_to helper, we don't need to specify the action, but button helper doesn`t support image tags