为什么 stylesheet_link_tag 在生产中不链接到 /assets ?
我刚刚对新的 Rails 3.1 应用程序进行了第一次部署,但资产似乎无法正常工作。我在部署时预编译所有内容,并且它按预期出现在 public/assets
中。但是,例如我的布局中 stylesheet_link_tag "application"
的输出有一个指向 /stylesheets/application.css
的 href。这显然是行不通的。
奇怪的是,在开发模式下一切似乎都很好,它像预期的那样链接到 /assets/application.css
。
我将我的 config/application.rb
和 config/environments/Production.rb
文件与资产管道上的另一个应用程序进行了比较,相关设置似乎是相同。
我应该去哪里看?
I just did the first deploy for a new Rails 3.1 application, but the assets don't seem to work correctly. I'm precompiling everything upon deploy, and it turns up in public/assets
like expected. However, the output of for example stylesheet_link_tag "application"
in my layout has a href pointing to /stylesheets/application.css
. This obviously does not work.
The strange thing is that in development mode everything seems to be fine, it links to /assets/application.css
like expected.
I've compared my config/application.rb
and config/environments/production.rb
files with another app we have on the asset pipeline, and the relevant settings seem to be the same.
Where should I look?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当帮助程序生成此代码时:
这是因为无论您处于何种模式,管道都未启用。有多种可能的原因。
您在错误的配置文件中启用了管道。 (应在 application.rb 配置文件中启用管道)
您在 application.rb 中启用它并在其他地方禁用它。
您不小心在某处注释了railtie
请参阅这个问题了解详细信息。
如果这是升级的应用程序,请检查
When the helper generates this code:
This is because the pipeline is NOT enabled in whatever mode you are in. There are several possible reasons.
You have the pipeline enabled in the wrong config file. (The pipeline should be enabled in the application.rb config file)
You have it enabled in application.rb and disabled somewhere else.
You have accidentally commented out a railtie somewhere
See this question for details.
If this is an upgraded app, check all the config options in the last section of the pipeline guide to make sure that you have them all correctly set.
我知道这是针对 Rails 3.1 的,但 Rails 4 的用户可能会发生非常类似的错误,因此为了完整起见并帮助未来的 Google 用户。
最可能的原因是您没有将该资产添加到 production.rb 中的预编译指令中,它看起来像这样:
正如OP所说,application.css 没有被添加,症状是 url 以 /stylesheets 或 / 开头javascript 而不是资产。所以解决办法是
I know this is for rails 3.1 but a very similar error can happen to users of rails 4 so for the sake of completeness and to help future googlers.
Most probable cause is you didn't added that asset to a precompile directive in production.rb, it looks like this:
As the OP said, application.css wasn't being added and the symptom is the url begins with /stylesheets or /javascripts and not assets. So the solution is to
警告:在生产环境中设置
config.assets.compile = true
可能会使您的应用容易受到Rails 资产管道目录遍历漏洞 (CVE-2018-3760).
我建议您在 production.rd 中将 config.assets.compile = false(默认情况下)启用为 true,并查看资产在developemnt中的服务情况。如果它们正确提供,那么您应该检查您的 application.css 以查看是否在目录中正确包含其他样式表,例如具有 css 文件,
其中脚手架和分页是 css 文件。或者在 config.assets.precompile 标志下提及它们,如下所示。
config.assets.precompile += %w(pagination.cssscaffold.css)
我认为原因是预编译有效(application.js、application.css 和所有非 JS/CSS 已添加)并且任何附加资产都应该是添加到 config.assets.precompile 标志。
WARNING: Setting
config.assets.compile = true
in production can make your app vulnerable to Rails Asset Pipeline Directory Traversal Vulnerability (CVE-2018-3760).
I would suggest you to enable config.assets.compile = false(by default) to true in production.rd and see the assets are served as in developemnt. If they are correctly served then you should check your application.css to see if you are including other stylesheets in the directory properly like having css files
where scaffold and pagination are css files. or mention them under config.assets.precompile flag as below.
config.assets.precompile += %w(pagination.css scaffold.css )
I assume the reason being precompile works (application.js, application.css, and all non-JS/CSS are already added) and any additonal assets should be added to config.assets.precompile flag.
我认为资产应该位于
app/assets
中,而不是public/assets
但我可能是错的。I thought that the assets were supposed to be in
app/assets
notpublic/assets
however I may be mistaken.