简体   繁体   English

Rails 6 不提供 static 文件

[英]Rails 6 not serving static files

I've just upgraded a rails 5.1 project to Rails 6 and finally got it to render views.我刚刚将一个 rails 5.1 项目升级到 Rails 6,最后让它渲染视图。 However, I still can't get Puma (4.3.3) to serve static files from the public/ folder.但是,我仍然无法让 Puma (4.3.3) 从public/文件夹中提供 static 文件。 I opted for using Sprockets 4.0 instead of Webpack due to the difficulty of setting it up.由于设置困难,我选择使用 Sprockets 4.0 而不是 Webpack。 I get this error:我收到此错误:

Sprockets::Rails::Helper::AssetNotFound - The asset "intro_video.mp4" is not present in the asset pipeline.:
  app/views/home/_intro_video.html.haml:1
  app/views/home/index.html.haml:52

Started POST "/__better_errors/82e7c6297114c48a/variables" for 127.0.0.1 at 2020-04-02 09:05:05 -0400

I'm in the development environment and have added config.serve_static_assets = true to my config/environments/development.rb and rebooted the server.我在开发环境中并已将config.serve_static_assets = true添加到我的config/environments/development.rb并重新启动服务器。 The page in question is linking to 'intro_video.mp4' which is in the root of the public/ folder.有问题的页面链接到位于public/文件夹根目录中的“intro_video.mp4”。 If I put localhost:3000/intro_video.mp4 directly into Firefox I get a ActionController::RoutingError: No route matches [GET] \"/intro_video.mp4\" error.如果我将localhost:3000/intro_video.mp4直接放入 Firefox 我得到一个ActionController::RoutingError: No route matches [GET] \"/intro_video.mp4\"错误。

Any help is appreciated.任何帮助表示赞赏。 Thank you.谢谢你。

EDIT: I've tried moving the intro_video.mp4 file to my app/assets/images directory but now I get this strange issue:编辑:我尝试将intro_video.mp4文件移动到我的app/assets/images目录,但现在我遇到了这个奇怪的问题:

SassC::SyntaxError at / SassC::SyntaxError 在 /

Error: Undefined variable: "$light-gray".错误:未定义的变量:“$light-gray”。 on line 15:10 of app/assets/stylesheets/datepicker.scss color: $light-gray;在 app/assets/stylesheets/datepicker.scss 的第 15:10 行颜色:$light-gray;

Which means that the @import 'variables' line in my application.scss does not work, but all the other files referenced there work.这意味着我的application.scss中的@import 'variables'行不起作用,但那里引用的所有其他文件都起作用。 In fact, if I add any files to my app/assets/images directory this happens.事实上,如果我将任何文件添加到我的app/assets/images目录,就会发生这种情况。 This makes no sense to me...这对我来说毫无意义...

EDIT (again): So I fixed the CSS issue by removing this from app/assets/config/manifest.js编辑(再次):所以我通过从app/assets/config/manifest.js中删除它来修复 CSS 问题

//= link_directory ../stylesheets .css

...which was affecting the load order of my stylesheets Still can't get anything to work from the public/ directory, so I'll just settle by moving everything to app/assets/images . ...这影响了我的样式表的加载顺序仍然无法从public/目录中获得任何工作,所以我将通过将所有内容移动到app/assets/images来解决。

For Rails 6, the following should work:对于 Rails 6,以下应该有效:

# config/environments/production.rb
config.public_file_server.enabled = true

I hope this doc suits you well.我希望这个文档适合你。

RAILS SERVE STATIC FILES with NGINX 导轨为 STATIC 文件提供 NGINX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM