JavaScript资产管道:404错误-如何在Rails中包含Google Analytics(分析)代码? [英] Javascript asset pipeline: 404 error - How to include Google analytics code in Rails?

查看:83
本文介绍了JavaScript资产管道:404错误-如何在Rails中包含Google Analytics(分析)代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google Analytics(分析)添加到我的Rails应用程序中,但是Rails的资产管道存在问题.

I'm trying to include Google Analytics in my Rails app but am having problems with Rails its asset pipeline.

我创建了一个新文件(app/assets/javascripts/google-analytics.js),其中包含了Google提供的Javascript代码.在application.html.erb中添加了以下行:javascript_include_tag 'google-analytics' if Rails.env.production?.

I created a new file (app/assets/javascripts/google-analytics.js) in which I included the Javascript code supplied by Google. To application.html.erb I added the line: javascript_include_tag 'google-analytics' if Rails.env.production?.

不幸的是,它在生产中不起作用.推送到Heroku之后,源代码包括:<script src="/javascripts/google-analytics.js"></script>.但是它找不到javascript文件,即,如果我在源代码中单击此链接,它将产生404错误页面.我在做什么错了?

Unfortunately it doesn't work in production. After pushing to Heroku, the source code includes: <script src="/javascripts/google-analytics.js"></script>. But it can't find the javascript file, i.e., if I click this link in the source code it produces the 404 error page. What am I doing wrong?

更新:如果我将Rails.application.config.assets.precompile += ['google-analytics.js']添加到production.rb,则可以使用.如果我将此行添加到assets.rb而不是production.rb,它现在可以正常工作.我希望将其放置在assets.rb中以保持其整洁.有谁知道为什么它不能与assets.rb一起使用?

Update: It works if I add Rails.application.config.assets.precompile += ['google-analytics.js'] to production.rb. It does now work if I add this line to assets.rb instead of production.rb. I would prefer to place it in assets.rb to keep it uncluttered. Does anyone understand why it isn't working with assets.rb?

将JS文件直接包含在文件夹/public/assets/google-analytics.js中是否是一个很好的选择?然后,我不需要将文件包含在/app/assets/javascripts/google-analytics.js中,也就不需要在production.rbassets.rb中使用预编译行了?

Would it be a good alternative to directly include the JS file in the folder /public/assets/google-analytics.js? Then I wouldn't need to include the file in /app/assets/javascripts/google-analytics.js and I wouldn't need the precompile lines in production.rb or assets.rb?

更新前的Heroku日志尾巴:

Heroku log tail before update:

heroku[router]: at=info method=GET path="/assets/application-1d520c66bc88***583e462611.css" host=***.herokuapp.com request_id=***  fwd="***" dyno=web.1 connect=0ms service=2ms status=304 bytes=93
heroku[router]: at=info method=GET path="/assets/application-2b1c01f66fb87e***bd707657bc1acf7.js" host=***.herokuapp.com request_id=*** fwd="***" dyno=web.1 connect=1ms service=2ms status=304 bytes=93
heroku[router]: at=info method=GET path="/javascripts/google-analytics.js" host=***.herokuapp.com request_id=*** fwd="***" dyno=web.1 connect=1ms service=4ms status=404 bytes=1708
app[web.1]: Started GET "/javascripts/google-analytics.js" for *** at 2015-07-15 11:11:23 +0000
app[web.1]: ActionController::RoutingError (No route matches [GET] "/javascripts/google-analytics.js"):
app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
etc.

推荐答案

Rails系统需要知道该文件的预编译. 默认情况下,它与application.js一起使用,但是如果您创建一个新的清单文件,则必须包含它.

Rails system needs to know pre-compilation of that file. By default it works with application.js but if you create a new manifest file, you have to include it.

config/initializers/assets.rbproduction.rb中添加以下行:

Rails.application.config.assets.precompile << ['google-analytics.js']

这篇关于JavaScript资产管道:404错误-如何在Rails中包含Google Analytics(分析)代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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