0

I am using ruby 2 and rails 4. I want to add http link into image link in rails. How can I create that?

My codes:

<% for g in @pictures %>                              
   <%= link_to image_tag g.pic_url, class: "img-responsive img-thumbnail" %> 
<% end %> 

I want to create something like below using rails.

<a href="/assets/image_001.jpg"><img src="/assets/image_001.jpg" class="img-responsive img-thumbnail"></a>

Please share with me if any one has any idea.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Priyank Dey
  • 1,104
  • 1
  • 19
  • 43

3 Answers3

2

The link_to helper can take a block of code, allowing you to do something like the following:

<% for g in @pictures %>
  <%= link_to g.pic_url do %>
    <%= image_tag g.pic_url, class: "img-responsive img-thumbnail" %>
  <% end %>
<% end %>

More info on the link_to helper can be found by looking through the Rails API documentation.

Hope it helps!

Zoran
  • 4,196
  • 2
  • 22
  • 33
1

my solution:

<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
Narasimha Reddy - Geeker
  • 3,510
  • 2
  • 18
  • 23
0

link_to take first argument as link's name and second argument is as url. in your code you have not assign url option. Change it as below

Try:

<%= link_to (image_tag(g.pic_url, class: "img-responsive img-thumbnail"), g.pic_url) %>

Ruby on Rails using link_to with image_tag

Community
  • 1
  • 1
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55