# Stylesheet linking and layouts

**URL:** <https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199>\
**Category:** Rails\
**Created:** [June 23, 2006, 12:15am UTC](https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199 "2006-06-23T00:15:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 23, 2006, 12:15am UTC](https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199/1 "2006-06-23T00:15:58Z")

</div>

In my layout, I have something like (shortened for clarity):

\<%= stylesheet\_link\_tag "index", :media =\> "all" %\>
 ... \<%= @content\_for\_layout %\> ... 

A view action has another stylesheet defined in the opening line such  
as:  
\<%= stylesheet\_link\_tag “view\_posts”, :media =\> “all” %\>

blah, blah, blah

The layout has the appropriate stylesheet for common layout features.  
The  
actions each have their own stylesheets so that the main stylesheet  
isn’t  
2000 lines long. 🙂 Now, the problem is that when I include a  
stylesheet  
link tag in the top of my action view, essentially that css definition  
is  
in a div tag on the overall page. The validator gives me a warning  
about  
this, but it does work. I’m sure there must be a more appropriate way  
to  
handle this, but I’m not sure what it might be. Any suggestions?

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 23, 2006, 12:25am UTC](https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199/2 "2006-06-23T00:25:16Z")

</div>

Hey David,

What I am about to suggest is not very elegant. I might go as far as  
“hacky”.

(1) In your controller.  
def my\_action  
@custom\_styles = [‘my\_action’,‘some\_other\_style’]  
# Do stuff  
end

(2) In you layout, just before the tag:  
\<% if @custom\_styles %\>  
\<% for s in @custom\_styles %\>  
\<%= stylesheet\_link\_tag s, :media =\> “screen” %\>  
\<% end %\>  
\<% end %\>

HTH  
~Rohith

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 23, 2006, 7:21am UTC](https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199/3 "2006-06-23T07:21:38Z")

</div>

I like Philip’s method better. It does not require creating a  
meaningless  
instance variable(and code) and it feels like “the right way” 😉

~R

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 23, 2006, 5:28am UTC](https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199/4 "2006-06-23T05:28:10Z")

</div>

> …   
> actions each have their own stylesheets so that the main stylesheet isn’t  
> 2000 lines long. 🙂 Now, the problem is that when I include a stylesheet  
> link tag in the top of my action view, essentially that css definition is  
> in a div tag on the overall page. The validator gives me a warning about  
> this, but it does work. I’m sure there must be a more appropriate way to  
> handle this, but I’m not sure what it might be. Any suggestions?

We do this in our layout:

\<%= yield “page\_styles” %\>

Then in our views do this:

\<% content\_for(“page\_styles”) do %\>  
\<%= stylesheet\_link\_tag “#{params[:controller]}/main” %\>  
\<%= stylesheet\_link\_tag “#{params[:controller]}/#{params[:action]}” %\>  
\<% end %\>

or whatever links to stylesheets you want…

Works for us.

-philip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 24, 2006, 6:24am UTC](https://www.ruby-forum.com/t/stylesheet-linking-and-layouts/64199/5 "2006-06-24T06:24:29Z")

</div>

It also works very well for putting javascript that absolutely has to go  
at the very end of the output because IE is stupid 🙂
