Zincktest
Zincktest

Reputation: 749

Rails, turning an image into a link

I want to turn my image into a link. But how? I read different answers but nothing worked.

 <%= link_to image_tag(article.images.first.image.url, :alt => article.title) unless 
      article.images.first.nil? %> 

I used this link to refer to the article by the title.

<%= link_to(article) do %><h2><%= article.title %></h2><% end %>
This works!  the path.     But i want the same path "article" with the image.

Upvotes: 0

Views: 123

Answers (2)

ecleel
ecleel

Reputation: 11868

This should work:

<%= link_to image_tag(article.images.first.image.url, :alt => article.title) unless 
  article.images.first.nil?, article %>

Upvotes: 0

Nich
Nich

Reputation: 1102

maybe you can do this ? do a check outside link_to

Upvotes: 1

Related Questions