Stylesheet linking and layouts

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. :slight_smile: 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

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

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

~R


actions each have their own stylesheets so that the main stylesheet isn’t
2000 lines long. :slight_smile: 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs