ruby on rails link_to an image

可紊 提交于 2019-12-28 05:52:55

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/18484543/ruby-on-rails-link-to-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!