`
fantaxy025025
  • 浏览: 1250650 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Rails的image_tag和image_path的使用

 
阅读更多

Rails的image_tag和image_path的使用

Rails 的 pipline,思想很好,不少细节考虑细致入微,但结构设计的跟屎一样,稍微脱离“小而简”的项目就没法好好工作了,没法说了。

 

#md5的fingerprint

http://guides.rubyonrails.org/asset_pipeline.html

具有pipline的好处。但是,发现有些图片没有生成指纹,为啥? 因为路径。

In regular views you can access images in the assets/images directory like this:

<%= image_tag "rails.png" %>

Provided that the pipeline is enabled within your application (and not disabled in the current environment context), this file is served by Sprockets. If a file exists at public/assets/rails.pngit is served by the web server.

Alternatively, a request for a file with an MD5 hash such as public/assets/rails-af27b6a414e6da00003503148be9b409.png is treated the same way. How these hashes are generated is covered in the In Production section later on in this guide.

 

 

#绝对路径和相对路径的区别

1.9.3-p194 :032 > helper.image_tag("/aaa/bbb.jpg")

 => "<img alt=\"Bbb\" src=\"/aaa/bbb.jpg\" />"

 

1.9.3-p194 :031 > helper.image_tag("aaa/bbb.jpg")

Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError: aaa/bbb.jpg isn't precompiled

只有相对路径的图片和静态资源,才会走指纹。

而且,如果关闭了生产环境的临时编译,一旦没有编译成功的静态资源,就报错了。 

具体的源码,有时间再贴上来。 

 

#实际的开发中,使用iamge_path较多

实际项目和环境,总是比闷头设计和想像要现实很多。

开发中, 我们不可能都把html改为rails的xxx_tag,所以直接想得到image_tag的图片路径,而不是生成的一堆字符串。找人问无果,就看源码,image_path正是。

 

*

*

*

-

*

*

*

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics