3

Instead of the page making another request for the CSS, I would like to have the Rails view render the CSS file in the page, so it's only 1 request.

Is this possible?

Zabba
  • 64,285
  • 47
  • 179
  • 207
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • Why not just use something like Jammit to compress all your CSS into one file? You can set very long expires headers on it, so users will download it once and then 304 it for next month or whatnot. – Chris Heald Feb 09 '11 at 10:41

3 Answers3

8

Altough the typical way to include CSS is using stylesheet_link_tag (in particular so it gets cached by the client), it is possible to put it directly in your <HEAD> in Rails 3.1:

<HEAD>
  ....
  <STYLE type="text/css">
    <%= YouAppName::Application.assets["your_stylesheet.css"].to_s.html_safe %>
  </STYLE>
</HEAD>

I adapted this from this post.

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
1

<%= stylesheet_link_tag :all %> assuming your CSS file is in your public/stylesheets folder (which is the conventional place to store stylesheets). Of course, instead of :all, you can specify a specific file.

in your page's block.

I usually do this in my application.html.erb (which is a layout in your app/views/layouts) folder. But you can do this in any view file with a block.

Good luck!

hwrd
  • 2,134
  • 6
  • 29
  • 36
0

Just wrap your CSS in

<style></style>

within your view.

seeingidog
  • 1,166
  • 9
  • 5
  • I want it in a separate css file in the stylesheets directories to keep things clean :) – AnApprentice Feb 08 '11 at 02:44
  • 1
    I see. Probably best to use content_for to prepare the stylesheet for later use. Similar to this: http://stackoverflow.com/questions/480288/how-to-include-a-css-or-javascript-in-an-erb-that-is-outside-the-layout – seeingidog Feb 08 '11 at 03:02