Rails 3.1 资产和布局问题
我有一个 Rails 3.1 应用程序,它按照标准做法在“/app/assets/stylesheets/application.css”中使用带有 css 的默认布局。这对于我的应用程序主要部分的资产管道工作非常有用。
不同的“报告”布局,如下所示:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "showreports" %>
<%= javascript_include_tag "showreports" %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="mainarea" class="container">
<div class="span-24 last">
<%= yield %>
</div>
</div>
</body>
但是,当我生成使用 /app/views/layouts/showreports.html.erb 布局的报告时,我使用 rake assets:precompile 预编译我的资产并检查清单文件 (/public/assets/manifest.yml) 中的内容是否正常,并且我没有看到任何对 showreports.css 或 showreports.js 的引用。
当我在开发模式下测试我的程序时,毫不奇怪,它无法找到这些文件。
我猜这是 Rails 3.1 的基本 sprocket 问题,但我认为 sprocket 足够智能,可以解析资产的所有布局文件(超出默认的 application.html.erb 文件)。
只是想知道这是否可能是一个错误,或者只是我对它应该如何工作的误解。
干杯,
格雷格
更新
经过更多的修改,我正在回答我自己的问题,因为这对我来说最有意义......
我在做什么错误是使用名为“showreports.css”和“showreports.js”的文件作为清单文件。
我的修复方法是创建 /app/assets/stylesheets/showreports
和 /app/assets/javascripts/showreports
,然后将我的 showreports.(css|js) 重命名为 application .(css|js) 在各自的目录中。
完成此操作后,rake assets:precompile
很好地找到了它们并将它们正确添加到清单文件中。
I have a rails 3.1 application that uses a default layout with css in "/app/assets/stylesheets/application.css" as per standard practice. This works great with the asset pipelining stuff for the main part of my application.
But I use a different "reporting" layout when I generate a report that uses a /app/views/layouts/showreports.html.erb layout as follows:
<!DOCTYPE html>
<html>
<head>
<%= stylesheet_link_tag "showreports" %>
<%= javascript_include_tag "showreports" %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="mainarea" class="container">
<div class="span-24 last">
<%= yield %>
</div>
</div>
</body>
I'm using rake assets:clean; rake assets:precompile
to pre-compile my assets and check things are looking good in the manifest file (/public/assets/manifest.yml) and I don't see any references to showreports.css or showreports.js.
When I test my program in dev-mode, unsurprisingly, it can't locate those files.
I'm guessing this is a basic sprockets question with rails 3.1 but I thought sprockets would be smart enough to parse all the layout files for assets (beyond the default application.html.erb file).
Just wondering if this may be a bug or just my misunderstanding on how this should work.
Cheers,
Greg
UPDATE
After more tinkering, I'm answering my own question as this made the most sense for me...
This answer really gave the right clue
What I was doing wrong was using files named "showreports.css" and "showreports.js" as manifest files.
My fix was to create /app/assets/stylesheets/showreports
and /app/assets/javascripts/showreports
and then to rename my showreports.(css|js) to application.(css|js) inside their respective directories.
After doing so, rake assets:precompile
found them nicely and added them correctly to the manifest file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 http://guides.rubyonrails.org/asset_pipeline.html 您还应该添加以下内容:
到您的 application.rb 文件
According to http://guides.rubyonrails.org/asset_pipeline.html you should also add following:
to your application.rb file
这是一个误会。 Rails Assets Pipeline 仅编译位于
RAILS_ROOT/app/assets
中的文件,您必须直接引用它们。如果您想生成另一个文件,只需创建另一个具有与 application.css/js 类似内容的清单文件。另请检查require
指令以避免将一个文件包含到另一个文件中。It's a misunderstanding. Rails Assets Pipeline only compiles files located in
RAILS_ROOT/app/assets
and you must reference them directly. If you want another file to be generated simply create another manifest file with similar content to application.css/js. Also checkrequire
directives to avoid including one file into the other.