Search code examples
ruby-on-railsimagelink-to

ruby on rails link_to an image


I'm facing the following problem: I have a very small image gallery with image files located in the following directories

 app/assets/images/locale/thumbs/
 app/assets/images/locale/big/

I have to create a hyperlink that as a content has a thumb image and as a target - its bigger version from app/assets/images/locale/big/ folder:

<a href="path-to-full-size-image-001.jpg">
 <img alt="first photo preview" src="/assets/locale/thumbs/001.jpg" />
</a>

I'm doing this by means of

= link_to(image_tag("locale/thumbs/001.jpg"),  "locale/big/spizzicaluna001.jpg") 

In fact I have tried many variants for the second argument of link_to but with little success - the bigger file can not be found.

How to resolve this issue?


Solution

  • There are two approches to this issue.

    1. You must specify the assets folder in the path.

      link_to( image_tag("locale/thumbs/001.jpg"), "/assets/locale/big/spizzicaluna001.jpg" )

    2. Use an image path allowing rails to find the correct image

      link_to( image_tag("locale/thumbs/001.jpg"), image_path( "locale/big/spizzicaluna001.jpg") )

    More info on image_path:

    http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_path