简体   繁体   中英

Disable Rails 3.1 asset pipeline for images only?

I'm upgrading a Rails 3 app to 3.2 and setting up the asset pipeline. It's great for css/js but I don't really see the point in using it for images, and unfortunately I have css with a ton of references /images/*.png and the like.

Is there a way to disable the asset pipeline just for images so image_tag("x.png") will go back to returning <img src="/images/x.png"> instead of <img src="/assets/x.png"> ? Thanks!

You can monkey-patch ActionView::Base, try this in rails console:

helper.image_path "foo" #=> "/assets/foo"

module OldImagePath
  def image_path(source)
    asset_paths.compute_public_path(source, 'images')
  end
  alias_method :path_to_image, :image_path
end
ActionView::Base.send :include, OldImagePath

helper.image_path "foo" #=> "/images/foo"

You can place this in an initializer for example. By default ActionView::Base includes ActionView::Helpers::AssetTagHelper and Sprockets::Helpers::RailsHelper which both define image_path but the latter take precedence. I'm including my own module which take precedence over all of them (the code inside is taken from ActionView::Helpers::AssetTagHelper).

Although, it makes sense to use asset pipeline for images too. They get hash sum in their filenames so that they can be cached forever on the client side without asking the server whether the file was changed.

Have a look at this gem: https://github.com/spohlenz/digestion , it should do what you need :).

Otherwise you can move the assets you don't want included in the asset pipeline from app/assets back into public (eg public/images). Everything should work as you expect without the need for a gem.

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