Adventures with Ruby

Asset paths

View Comments

Just a quickie. We all know that Rails adds asset ids to images, javascript include tags and stylesheet link tags. This is to help the browser in caching assets properly. The generated code looks something like this:

<img src="rails.png?20849923" alt="Rails" />

If I wonder if a website was made in Rails, I always look at the HTML. This is a good indicator.

Sometimes you don’t want this behavior. It can get in the way of wget, which saves the file including everything after the question mark. But it’s not that straightforward to turn this off. At least, I couldn’t find it. There is not much information available on this subject and I had to dive into the Rails source code to solve it.

The easiest way I found was to override a helper method:

module ApplicationHelper
  def rewrite_asset_path(source)
    source
  end
end

Please correct me if there is a simpler way!

Written by Iain Hecker

November 4th, 2008 at 12:06 am

Posted in Uncategorized

  • Erik

    Great! How can I make that override conditional? I only want to override if in development environment.

  • Erik

    Great! How can I make that override conditional? I only want to override if in development environment.

  • http://iain.nl Iain Hecker

    This should do it (out of the top of my head):

    module ApplicationHelper
      def rewrite_asset_path(source)
        Rails.env.development? ? source : super(source)
      end
    end
  • http://iain.nl Iain Hecker

    This should do it (out of the top of my head):

    module ApplicationHelper
      def rewrite_asset_path(source)
        Rails.env.development? ? source : super(source)
      end
    end
blog comments powered by Disqus
Fork me on GitHub