14 Jun 2011

When your assets doesn't get an asset_id...

… check that the asset does exist.

I had the following code in my application layout template:

    <!DOCTYPE html>
    <html>
    <head>
      <title>My webpage title</title>
      <%= stylesheet_link_tag 'default' %>
      <%= stylesheet_link_tag @current_subdomain %>
      <%= javascript_include_tag :defaults %>
      <%= csrf_meta_tag %>
    </head>

It works great to load a custom stylesheet depending on the current subdomain, however I noticed that for some subdomains Rails was not adding the asset_id:

    <link type="text/css" rel="stylesheet" media="screen" href="/stylesheets/default.css?1307960871">
    <link type="text/css" rel="stylesheet" media="screen" href="/stylesheets/sample.css">

It is because sample.css must exist. stylesheet_link_tag does not check for the existence of the asset, and if it doesn’t exists it doesn’t add the asset timestamp.

Hope it saves you some time.