stylesheet_link_tag
stylesheet_link_tag(*sources)
public
Returns a stylesheet link tag for the sources specified as arguments. If you don’t specify an extension, .css will be appended automatically. You can modify the link attributes by passing a hash as the last argument.
Examples
stylesheet_link_tag "style" # => <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" /> stylesheet_link_tag "style.css" # => <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" /> stylesheet_link_tag "http://www.railsapplication.com/style.css" # => <link href="http://www.railsapplication.com/style.css" media="screen" rel="stylesheet" type="text/css" /> stylesheet_link_tag "style", :media => "all" # => <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" /> stylesheet_link_tag "style", :media => "print" # => <link href="/stylesheets/style.css" media="print" rel="stylesheet" type="text/css" /> stylesheet_link_tag "random.styles", "/css/stylish" # => <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" /> <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
You can also include all styles in the stylesheets directory using :all as the source:
stylesheet_link_tag :all # => <link href="/stylesheets/style1.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/styleB.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/styleX2.css" media="screen" rel="stylesheet" type="text/css" />
If you want Rails to search in all the subdirectories under stylesheets, you should explicitly set :recursive:
stylesheet_link_tag :all, :recursive => true
Caching multiple stylesheets into one
You can also cache multiple stylesheets into one file, which requires less HTTP connections and can better be compressed by gzip (leading to faster transfers). Caching will only happen if config.perform_caching is set to true (which is the case by default for the Rails production environment, but not for the development environment). Examples:
Examples
stylesheet_link_tag :all, :cache => true # when config.perform_caching is false => <link href="/stylesheets/style1.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/styleB.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/styleX2.css" media="screen" rel="stylesheet" type="text/css" /> stylesheet_link_tag :all, :cache => true # when config.perform_caching is true => <link href="/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" /> stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when config.perform_caching is false => <link href="/stylesheets/shop.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/cart.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/checkout.css" media="screen" rel="stylesheet" type="text/css" /> stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when config.perform_caching is true => <link href="/stylesheets/payment.css" media="screen" rel="stylesheet" type="text/css" />
The :recursive option is also available for caching:
stylesheet_link_tag :all, :cache => true, :recursive => true
To force concatenation (even in development mode) set :concat to true. This is useful if you have too many stylesheets for IE to load.
stylesheet_link_tag :all, :concat => true
Assets hosts
You can also setup assets hosts in enviroments:
config.action_controller.asset_host = "http://your-assets-server.com"