简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM