生产中的Rails资产无法使用(还需要发行另一项资产) [英] rails assets in production not served (yet another assets issue)

查看:67
本文介绍了生产中的Rails资产无法使用(还需要发行另一项资产)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 阿帕奇2.2.15 乘客5.1.2 导轨4.2.6 红宝石2.3.3

I'm using apache 2.2.15 passenger 5.1.2 rails 4.2.6 ruby 2.3.3

在rails日志中,我得到

In the rails log, I get

(No route matches [GET] application-xxxxx.js

以及

(No route matches [GET] application-xxxxx.css

我可以在public/assets目录中找到资产.

I can find the assets in the public/assets directory.

我的清单:

app/assets/stylesheets/application.css.less:

app/assets/stylesheets/application.css.less:

/*
 *= require jquery-ui
 *= require font-awesome
 *= require bootstrap_and_overrides
 *= require awesome-bootstrap-checkbox
 *= require lists
 *= require mailgroups
 *= require general
 *= require colors
 *= require print
 *= require loader
 *= require tooltips
 *= require_self
 *= require_tree .
*/

app/assets/javascripts/application.js

app/assets/javascripts/application.js

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery-ui
//= require twitter/bootstrap
//= require bootstrap
//= require turbolinks
//= require twitter/bootstrap/rails/confirm
//= require_self
//= require_tree .

然后我在config/production.rb中设置(在SO上遵循其他答案)

and I set (following other answers here on SO) in config/production.rb

config.serve_static_files = true

在app/views/layouts/application.html.erb

in app/views/layouts/application.html.erb

<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => "reload" %>
<%= javascript_include_tag "application", "data-turbolinks-track" => "reload" %>

我迷路了,谢谢你的帮助.

I'm lost, thanks for your help.

更新:我不使用Capistrano,只是手动部署.

UPDATE: I don't use Capistrano, just deploying manually.

在rails控制台中运行Rails.application.config.assets.paths时,我得到以下信息:

When running Rails.application.config.assets.paths in the rails console, I get the following:

 => ["/var/www/myappname/app/assets/fonts",
 "/var/www/myappname/app/assets/images",
 "/var/www/myappname/app/assets/javascripts",
 "/var/www/myappname/app/assets/stylesheets",
 "/var/www/myappname/vendor/assets/fonts",
 "/var/www/myappname/vendor/assets/images",
 "/var/www/myappname/vendor/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-confirm-1.0.6/vendor/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/local_time-1.0.3/app/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/font-awesome-rails-4.7.0.1/app/assets/fonts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/font-awesome-rails-4.7.0.1/app/assets/stylesheets",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-fontawesome-0.5.1/vendor/assets/fonts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-fontawesome-0.5.1/vendor/assets/stylesheets",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-bootstrap-3.3.5.0/app/assets/fonts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-bootstrap-3.3.5.0/app/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-bootstrap-3.3.5.0/app/assets/stylesheets",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/fonts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/images",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/stylesheets",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/vendor/assets/stylesheets",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-turbolinks-2.1.0/vendor/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-ui-rails-6.0.1/app/assets/images",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-ui-rails-6.0.1/app/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-ui-rails-6.0.1/app/assets/stylesheets",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-rails-4.2.2/vendor/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/coffee-rails-4.0.1/lib/assets/javascripts",
 "/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/turbolinks-source-5.0.0/lib/assets/javascripts",
 #<Pathname:/var/www/myappname/app/assets/fonts>,
 #<Pathname:/var/www/myappname/app/views>,
 #<Pathname:/var/www/myappname/app/assets/stylesheets>,
 #<Pathname:/var/www/myappname/app/assets/javascripts>]

我的config/initializers/assets.rb已经包含以下内容:

my config/initializers/assets.rb already contains this:

Rails.application.config.assets.precompile += ['.svg', '*.eot', '*.woff', '*.woff2', '*.ttf', '*.otf', '*.js', '*.css']

推荐答案

这是我的建议.

我添加了以下资源,可能有助于寻找解决方案.

I add the following resources that may help in finding a solution.

https://launchschool.com/blog/rails-asset-pipeline -最佳做法

https://railsapps.github.io/rails-javascript-include -external.html

https://reinteractive.com/posts/116-12-tips-for-rails-asset-pipeline

我阅读了通过搜索Rails Asset Pipeline可以找到的所有指南,但我不会将它们全部链接起来

I read all the guides that can be found by searching rails asset pipeline, but I will not link them all

我的建议是使用Chrome开发者工具检查application.css和application.js的指纹(进入任何屏幕元素,使用f12打开开发者工具箱,并检查任何div或任何js文件的样式).您将需要检查使用文件的哪个指纹版本,然后使用文本编辑器打开该版本,并检查文件的指纹版本中加载了什么.

My advice is to check the fingerprint of the application.css and application.js both with the Chrome Developer Tools (by going in any screen element, opening the developer toolbox with f12 and checking any style of any div or any js file). You will need to check which fingerprint version of the file is used, then open that version with the text editor and check what is loaded in the fingerprint version of the file.

如果将文件(例如,user.js文件)加载到清单的底部,则应该在已指纹识别的application.js底部轻松找到该文件.

If your file (for ex. user.js) is loaded at the bottom of the manifest, you should easily find it at the bottom of your fingerprinted application.js.

如果要更新此文件,则可以运行

If you want to update this file you can run

rake assets:precompile

这将仅在开发中预编译资产,对于生产,您需要指定环境.它将通过您完成的编辑更改指纹文件application.css和application.js.

This will precompile assets only in development, for production you need to specify the environment. It will change that fingerprinted file application.css and application.js with the editing you have done.

RAILS_ENV=production bundle exec rake assets:precompile

您还可以在以下地址localhost:3000/assets/yourjsfile.js上检查文件,或通过localhost:3000/assets/application-yourfingerprint.js

You can also check the file at the following address localhost:3000/assets/yourjsfile.js or check the fingerprint version by following localhost:3000/assets/application-yourfingerprint.js

如您所见,该public/assets文件夹中包含许多文件,您可以使用rake assets:clean-rf public/assets删除它们,然后运行rake assets:precompile,它将再次预编译资产.

As you can see, many files are included in that public/assets folder, you can delete them with rake assets:clean or -rf public/assets and then run rake assets:precompile, it will precompile again assets.

如果不包含任何内容,则说明您的application.js没有被加载.当我在其他帖子上阅读时,您可以尝试以下操作:

If nothing is included, then your application.js is not being loaded. You may try the following as I read on different posts:

  1. 使用以下代码行将文件包含在config/initializers/assets.rb中:

Rails.application.config.assets.precompile += %w( user.js )

  • 启动rails c并运行Rails.application.config.assets.paths 此命令将以红色显示自动包含的路径,例如App,Lib和Vendor内部的路径,以绿色显示您通过在config/application.rb内部添加以下代码以添加新路径而包含的路径

  • Start the rails c and run Rails.application.config.assets.paths This command will show in red the paths that are automatically included, for example those inside App, Lib and Vendor, in green those that you included by adding inside config/application.rb the follwing code to add new path

    config.assets.paths << Rails.root.join("vendor","assets", "fonts")
    

  • 我过去犯的另一个错误是未使用正确的链轮语法,您可以在以下链接中进行检查(我使用的是require_user而不是require user):

    Another error i made in the past was not using the correct sprockets syntax, you can check it at the following link ( i was using require_user instead of require user):

    https://github.com/rails/sprockets#sprockets-directives

    另一个问题是我在App/assets/javascript下有两个名为user的文件(user.coffee和user.js).第一个要加载的是user.coffee,它是空的.

    Another problem was that I had under App/assets/javascript two files named user (user.coffee and user.js). The first to be loaded was user.coffee which was empty.

    这篇关于生产中的Rails资产无法使用(还需要发行另一项资产)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆