Method: ActionView::Helpers::AssetTagHelper#stylesheet_link_tag
- Defined in:
- lib/action_view/helpers/asset_tag_helper.rb
#stylesheet_link_tag(*sources) ⇒ Object
Returns a stylesheet link tag for the sources specified as arguments.
When passing paths, the .css extension is optional. If you don’t specify an extension, .css will be appended automatically. If you do not want .css appended to the path, set extname: false in the options. You can modify the link attributes by passing a hash as the last argument.
If the server supports HTTP Early Hints, Rails will push a 103 Early Hints response that links to the assets.
Options
-
:extname- Append an extension to the generated URL unless the extension already exists. This only applies for relative URLs. -
:protocol- Sets the protocol of the generated URL. This option only applies when a relative URL andhostoptions are provided. -
:host- When a relative URL is provided the host is added to the that path. -
:skip_pipeline- This option is used to bypass the asset pipeline when it is set to true. -
:nonce- When set to true, adds an automatic nonce value if you have Content Security Policy enabled. -
:nopush- Specify if the use of server push is not desired for the stylesheet. Defaults totrue.
Examples
stylesheet_link_tag "style"
# => <link href="/assets/style.css" rel="stylesheet" />
stylesheet_link_tag "style.css"
# => <link href="/assets/style.css" rel="stylesheet" />
stylesheet_link_tag "http://www.example.com/style.css"
# => <link href="http://www.example.com/style.css" rel="stylesheet" />
stylesheet_link_tag "style.less", extname: false, skip_pipeline: true, rel: "stylesheet/less"
# => <link href="/stylesheets/style.less" rel="stylesheet/less">
stylesheet_link_tag "style", media: "all"
# => <link href="/assets/style.css" media="all" rel="stylesheet" />
stylesheet_link_tag "style", media: "print"
# => <link href="/assets/style.css" media="print" rel="stylesheet" />
stylesheet_link_tag "random.styles", "/css/stylish"
# => <link href="/assets/random.styles" rel="stylesheet" />
# <link href="/css/stylish.css" rel="stylesheet" />
stylesheet_link_tag "style", nonce: true
# => <link href="/assets/style.css" rel="stylesheet" nonce="..." />
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/action_view/helpers/asset_tag_helper.rb', line 207 def stylesheet_link_tag(*sources) = sources..stringify_keys = .extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys use_preload_links_header = ["preload_links_header"].nil? ? preload_links_header : .delete("preload_links_header") preload_links = [] crossorigin = .delete("crossorigin") crossorigin = "anonymous" if crossorigin == true nopush = ["nopush"].nil? || .delete("nopush") integrity = ["integrity"] = sources.uniq.map { |source| href = path_to_stylesheet(source, ) if use_preload_links_header && href.present? && !href.start_with?("data:") preload_link = "<#{href}>; rel=preload; as=style" preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil? preload_link += "; integrity=#{integrity}" unless integrity.nil? preload_link += "; nonce=#{content_security_policy_nonce}" if ["nonce"] == true preload_link += "; nopush" if nopush preload_links << preload_link end = { "rel" => "stylesheet", "crossorigin" => crossorigin, "href" => href }.merge!() if ["nonce"] == true || (!.key?("nonce") && auto_include_nonce_for_styles && respond_to?(:content_security_policy_nonce)) ["nonce"] = content_security_policy_nonce elsif ["nonce"] == false .delete("nonce") end if apply_stylesheet_media_default && ["media"].blank? ["media"] = "screen" end tag(:link, ) }.join("\n").html_safe if use_preload_links_header send_preload_links_header(preload_links) end end |