Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 396765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:35:04+00:00 2026-05-12T16:35:04+00:00

So Rails time stamping is great. I’m using it to add expires headers to

  • 0

So Rails time stamping is great. I’m using it to add expires headers to all files that end in the 10 digit timestamp. Most of my images however are referenced in my CSS. Has anyone come across any method that allows for timestamps to be added to CSS referenced images, or some funky re-write rule that achieves this? I’d love for ALL images in my site, both inline and in css to have this timestamp so I can tell the browser to cache them, but refresh any time the file itself changes.

I couldn’t find anything on the net regarding this and I can’t believe this isn’t a more frequently discussed topic.

I don’t think my setup will matter because the actual expiring will hopefully happen the same way, based on the 10 digit timestamp, but I’m using apache to serve all static content if that matters

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:35:04+00:00Added an answer on May 12, 2026 at 4:35 pm

    You can append the actual timestamp of each image file by getting the file modification time like this:

        source.gsub!(/url\((['"]*)(.+)(['"]*)\)/) do
          open, file, close = $1, $2, $3
          css_dir = File.join(RAILS_ROOT,"public/stylesheets")
          timestamp = ''
          FileUtils.cd(css_dir) do 
            if file =~ /^\// # absolute path
              f = File.new(RAILS_ROOT + "/public" + file)
            else # relative path
              f = File.new(file)
            end
            timestamp = f.mtime.to_i.to_s
          end
    
          if file =~ /.\.(ico|css|js|gif|jpe?g|png)/
            "url(#{open}#{file}?#{timestamp}#{close})"
          else
            "url(#{open}#{file}#{close})"
          end
        end
    

    (There’s probably a more elegant way to write this, my ruby chops are still weak!)
    Now the hack’s getting ugly, though… you’d think there would be a more Rails-like way to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to deploy a website the first time using Rails 3.2.1 with capistrano
My Rails application makes use of Time.zone so that each user can set their
I'm exploring Rails for the first time and trying to add some fairly straightforward
Using Rails ActiveRecord, am wanting to calculate the time between two datetime fields within
I've got a Rails app that's been running live for some time, and I'm
I'm using backbone.js and rails. When backbone.js saves a model for the first time
First time user of sending android Push messages from rails server. Using https://github.com/sghael/speedy_c2dm Have
I'm using Devise for the first time with rails, and I'm having trouble with
In Rails 3.0.10 I'm using Time.zone to change how a user sees times of
First time using rails, I'm trying to create a download link to get something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.