Javascript Asset Console: Error 404. How do I enable Google Analytics code in Rails?

I am trying to incorporate Google Analytics into my Rails application, but I am having problems with the Rails pipeline.

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

Unfortunately, he does not work in production. After clicking on Heroku source code includes: <script src="/javascripts/google-analytics.js"></script>. But he cannot find the javascript file, that is, if I click on this link in the source code, it gives an error page 404. What am I doing wrong?


Update: Works if I add Rails.application.config.assets.precompile += ['google-analytics.js']in production.rb. Now it works if I add this line to assets.rbinstead production.rb. I would rather put it in assets.rbso that it stays unpolluted. Does anyone understand why it is not working with assets.rb?

Would it be a good alternative to directly include the JS file in the folder /public/assets/google-analytics.js? Then I would not need to include the file in /app/assets/javascripts/google-analytics.js, and I do not need precompilation lines in production.rbor assets.rb?


Heroku magazine tail before upgrade:

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.
+1
source share
2 answers

The Rails system must know the preliminary compilation of this file. By default, it works with application.jsbut if you create a new manifest file, you must include it.

config/initializers/assets.rb production.rb config/initializers/assets.rb :

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

?

//= require google-analytics.js

application.js,

rake assets:precompile

.

0

Source: https://habr.com/ru/post/1598240/


All Articles