在 Rails 3.1 资产管道中调用媒体特定 css 的正确方法

发布于 2025-01-07 04:49:22 字数 983 浏览 3 评论 0原文

如果使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-01-14 04:49:22

问题在于方法调用的语法。

stylesheet_link_tag 'application', :media => 'screen, projection'
stylesheet_link_tag 'application-print', :media => 'print'

在您的代码中,您分配给一个名为 media 的局部变量

The problem is the syntax for the method call.

stylesheet_link_tag 'application', :media => 'screen, projection'
stylesheet_link_tag 'application-print', :media => 'print'

In your code you were assigning to a local variable called media

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文