在 Rails 3.1 资产管道中调用媒体特定 css 的正确方法
如果使用 Rails 3.1 资产管道,调用特定媒体(即打印、屏幕等)CSS 的正确/rails/最佳实践方法是什么?我知道以前有人问过这个问题,但我见过的所有解决方案似乎都很老套,而不是我所期望的优雅的 Rails 解决方案。
据我所知,建议的方法是按如下方式设置样式表文件夹:
assets
-stylesheets
--application.css
--application-print.css
--print
---custom-print.css
--screen
---custom-screen.css
application.css 的内容是
/*
*= require_self
*= require_tree ./screen
*/
application-print.css 的内容,然后
/*
*= require_self
*= require_tree ./print
*/
在布局中包含以下内容
<%= stylesheet_link_tag 'application', media = 'screen, projection' %>
<%= stylesheet_link_tag 'application-print', media = 'print' %>
好的,到目前为止一切顺利。
但是,就我而言,custom-print.css 正在屏幕上应用,并且没有 css 应用于打印输出。
此外,这种方法似乎会影响从 css 调用的图像。即现在不是在资产/图像中查找图像,而是在资产/样式表/屏幕中查找图像。这里可能还有其他问题,因为似乎只有 javascript css 受到影响。我会做更多检查并报告。
所以我的问题是,你们如何处理 Rails 资产管道中媒体特定的 css?什么被认为是最佳实践?我做错了什么?
感谢您抽出时间!
What is the correct/rails/best practice way to call media specific (i.e. print, screen, etc) css if using Rails 3.1 asset pipeline? I know this has been asked before, but all of the solutions I've seen seem very hacky and not the elegant Rails solution I've come to expect.
As far as I can tell, the suggested approach is to set up the stylesheets folder as follows:
assets
-stylesheets
--application.css
--application-print.css
--print
---custom-print.css
--screen
---custom-screen.css
the contents of application.css being
/*
*= require_self
*= require_tree ./screen
*/
the contents of application-print.css being
/*
*= require_self
*= require_tree ./print
*/
then to include the following in the layout
<%= stylesheet_link_tag 'application', media = 'screen, projection' %>
<%= stylesheet_link_tag 'application-print', media = 'print' %>
OK, so far so good.
BUT, in my case custom-print.css is being applied onscreen, and no css is being applied to printed output.
Also, this approach seem to affect images called from css. i.e. instead of looking for images in assets/images it is now looking for images in assets/stylesheets/screen. There may be something else going on here, as it seems to be only javascript css that is affected. I will do some more checking and report back.
So my question is, how are you dealing with media specific css in the Rails asset pipeline?What is regarded as best practice? And what am I doing wrong?
Thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于方法调用的语法。
在您的代码中,您分配给一个名为
media
的局部变量The problem is the syntax for the method call.
In your code you were assigning to a local variable called
media