Priyank Dey
Priyank Dey

Reputation: 1124

How to add http link on image tag using rails?

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.

Upvotes: 0

Views: 1114

Answers (3)

Gagan Gami
Gagan Gami

Reputation: 10251

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

Upvotes: 0

Narasimha Reddy - Geeker
Narasimha Reddy - Geeker

Reputation: 3860

my solution:

<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some css class here" %>
<% end %>

Upvotes: 1

Zoran
Zoran

Reputation: 4226

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!

Upvotes: 2

Related Questions