Style sheet partial

by Adam Maddox Jul 10th, 2007 at 8:23 PM

A while back i noticed that my css changes were not taking effect on production (after running cap deploy). A quick check on my css inclusion method revealed the problem. I was using a single application.css file to import all css files through the use of import.

The reason for the stagnant styles? Well, my understanding is (feel free to correct me) the browser caches the static imports with a long expire time. So, following a deploy, several pages views will appear the same as before.

A next step i guess is caching the included stylesheets. This appears to be an upcomming feature of Rails 2.0.

Using @import

app/views/layouts/Application.rb (in the head tag)

<%= stylesheet_link_tag "application" %>

public/stylesheets/application.css

@import "base.css";

/* Defaults */
@import "grid.css";
@import "table.css";
@import "tabs.css";
.
.
etc

Using a partial

app/views/layours/Application.rb (in the head tag)

<%= render :partial => 'layouts/styles' %>

app/views/layouts/_styles.rhtml


<% Dir["public/stylesheets/*.css"].each do |style| %>
    <%= stylesheet_link_tag File.basename(style) %>
<% end %>

Comments

There is 1 comment for this article

  • Avatar

    Matt Johnson

    21 Aug 14:11

    Good idea, I'm going to use it now. Any reason for putting the partial in layouts rather than somewhere like apps/views/shared?

Comments are closed.