Ruby-on-rails Stylesheet_link_tag :all 与 :media =>all
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15805810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stylesheet_link_tag :all versus :media =>all
提问by Lucy Weatherford
I created a new Rails application from a scaffold, but the tutorial claims the following will appear:
我从脚手架创建了一个新的 Rails 应用程序,但教程声称会出现以下内容:
<%= stylesheet_link_tag "application", :media => "all" %>
while I got:
当我得到:
<%= stylesheet_link_tag :all %>
What is the difference between them? Which should I use? Why?
它们之间有什么区别?我应该使用哪个?为什么?
回答by rorra
Using
使用
<%= stylesheet_link_tag "application", :media => "all" %>
will include the stylesheet named application.css, you can have files like application.css.sassor application.css.scssor any other extensions and rails will compile the css file with the right stylesheet engine and serve the application.css file.
将包含名为application.css的样式表,您可以拥有application.css.sass或application.css.scss或任何其他扩展名等文件,rails 将使用正确的样式表引擎编译 css 文件并提供 application.css 文件。
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.
属性“media=all”实际上是一个css属性,意思是所有的媒体都会包含css,比如浏览网站的时候,打印屏幕的时候等等。你可以在这个找到关于media属性的更多信息关联。
By using
通过使用
<%= stylesheet_link_tag :all %>
you will include all the stylesheets that you have on your app/assets/stylesheetsdirectory.
您将包含app/assets/stylesheets目录中的所有样式表。
You can find more information on this link.
回答by Marcin Pietraszek
Please look at api docs. Here you have some quote from it:
请查看api 文档。这里有一些引述:
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" />
回答by Kees Sonnema
stylesheet_link_tag also accepts a parameter for the media attribute in the generated
stylesheet_link_tag 还接受生成的媒体属性的参数
< %= 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:
相反,如果您想在 stylesheets 目录中包含所有样式表,只需调用:
< %= stylesheet_link_tag :all %>
from the rails wiki:
来自 Rails 维基:
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:
请注意,默认情况下,stylesheet_link_tag 将在应用程序的 /public/stylesheets 目录中搜索样式表。此外,当没有其他参数传递给 stylesheet_link_tag 时,type 属性设置为 text/css,media 设置为 screen,relationship 设置为 stylesheet。此外,在传递文件名参数时,您不必在文件名中包含 .css 扩展名。欢迎您包含它,但只要您的 CSS 样式表以 .css 扩展名命名,就没有必要这样做。请注意,如果您包含文件扩展名,Rails 将不再为此调用查找具有 .css 扩展名的文件。例如,如果您想包含一个名为 my_style.new.css 的样式表,以下内容是不够的:
回答by Igor Kapkov
The second one not about media type, it's mean include all .css from stylesheets directory on non asset pipeline project.
第二个与媒体类型无关,这意味着在非资产管道项目中包含样式表目录中的所有 .css。
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" />

![Ruby-on-rails Rails 中的 ActionController::RoutingError(没有路由匹配 [GET]](/res/img/loading.gif)