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.

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

my solution:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.