2005-10-08

Ooh, this is nice!

Another helpful tip from Demetrius Nunes:

Drop the following method in your application_helper.rb file.


def stylesheet_auto_link_tags
stylesheets_path = "#{RAILS_ROOT}/public/stylesheets/"
candidates = [
"#{controller.controller_name}",
"#{controller.controller_name}_#{controller.action_name}" ]
candidates.inject("") do |buf, css|
buf <<>
"#{stylesheets_path}/#{css}.css")
buf
end
end

Next, drop <%= stylesheet_auto_link_tags %> in the of your layout file.

Now, if you have a controller foo, create a stylesheet called foo.css in public\stylesheets and it will automatically be linked to (but only if it exists) in all views rendered by that controller.

Likewise, if you have a style used only by action bar of controller foo, name your file foo_bar.css.

You will obviously want to make some styles global to a group of controllers or your entire application, but you can manage complexity and reduce unwanted interactions by scoping your stylesheets down this way.