Stylesheet_link_tag :all versus :media =>all

风流意气都作罢 提交于 2019-12-18 11:56:06

问题


I created a new Rails application from a scaffold, but the tutorial claims the following will appear:

<%= stylesheet_link_tag    "application", :media => "all" %>

while I got:

<%= stylesheet_link_tag    :all %>

What is the difference between them? Which should I use? Why?


回答1:


Using

<%= stylesheet_link_tag    "application", :media => "all" %>

will include the stylesheet named application.css, you can have files like application.css.sass or application.css.scss or any other extensions and rails will compile the css file with the right stylesheet engine and serve the application.css file.

The attribute "media=all" is actually a css attribute, which means that the css will be included for all the medias, like when browsing the website, when printing the screen, etc. You can find more information about the media attribute on this link.

By using

<%= stylesheet_link_tag    :all %>

you will include all the stylesheets that you have on your app/assets/stylesheets directory.

You can find more information on this link.




回答2:


Please look at api docs. Here you have some quote from it:

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" />


stylesheet_link_tag "style", :media => "all" # =>
  <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />



回答3:


The second one not about media type, it's mean include all .css from stylesheets directory on non asset pipeline project.

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" />



回答4:


stylesheet_link_tag also accepts a parameter for the media attribute in the generated

 < %= stylesheet_link_tag :media => "all" %>

Will produce:

<link href="/stylesheets/killer.css" media="all" rel="stylesheet" type="text/css" />

If, instead, you want to include all the stylesheets in the stylesheets directory, just call:

< %= stylesheet_link_tag :all %>

from the rails wiki:

Notice that stylesheet_link_tag will, by default, search for stylesheets in the /public/stylesheets directory of your application. Also, when no other parameters are passed to stylesheet_link_tag, the type attribute is set to text/css, media is set to screen, and relationship is set to stylesheet. Furthermore, you don't have to include the .css extension in the filename when passing the filename parameter. You're welcome to include it, but as long as your CSS stylesheets are named with the .css extension, there's no need to do so. Note that if you include a file extension, Rails will no longer look for a file with the .css extension for this call. For instance, if you want to include a stylesheet named my_style.new.css, the following is not sufficient:



来源:https://stackoverflow.com/questions/15805810/stylesheet-link-tag-all-versus-media-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!