Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Mar 24 ’10

Automatically link a controller’s stylesheet

This is a simple application helper that shaunchapman wrote to reduce the tediousness of linking stylesheets for each controller. It will link the controller’s stylesheet (located at public/stylesheets/[controller_name].css) if it exists but it won’t complain if it doesn’t. It is a nice way to keep things organized and it’s super simple to set up.

Simply add this to your application helper (located at app/helpers/application_helper.rb):

def controller_stylesheet_link_tag
  stylesheet = "#{params[:controller]}.css"
    
  if File.exists?(File.join(Rails.public_path, 'stylesheets', stylesheet))
    stylesheet_link_tag stylesheet
  end
end

…and put this in your application layout (located at app/views/layouts/application.html.erb):

<%= controller_stylesheet_link_tag %>

73 notes 0 comments (via shaunchapman)

  1. -buy-viagra-online-uk- reblogged this from shaunchapman
  2. download-audio-books-cheap reblogged this from shaunchapman
  3. morethanaprogrammer reblogged this from shaunchapman
  4. lucashungaro reblogged this from rubyquicktips
  5. sanemat reblogged this from rubyquicktips
  6. atm09td reblogged this from rubyquicktips
  7. jpunt reblogged this from rubyquicktips
  8. rubyquicktips reblogged this from shaunchapman and added:
    that shaunchapman...It is a nice way...organized and it’s
  9. shaunchapman posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus