i'm trying include google analytics in rails app having problems rails asset pipeline.
i created new file (app/assets/javascripts/google-analytics.js) in included javascript code supplied google. application.html.erb added line: javascript_include_tag 'google-analytics' if rails.env.production?.
unfortunately doesn't work in production. after pushing heroku, source code includes: <script src="/javascripts/google-analytics.js"></script>. can't find javascript file, i.e., if click link in source code produces 404 error page. doing wrong?
update: works if add rails.application.config.assets.precompile += ['google-analytics.js'] production.rb. work if add line assets.rb instead of production.rb. prefer place in assets.rb keep uncluttered. understand why isn't working assets.rb?
would alternative directly include js file in folder /public/assets/google-analytics.js? wouldn't need include file in /app/assets/javascripts/google-analytics.js , wouldn't need precompile lines in production.rb or assets.rb?
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 "/javascripts/google-analytics.js" *** @ 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.
you have tell rails precompile file. default works application.js if create new manifest file, have include it. in config/initializers/assets.rb or production.rb
add:
rails.application.config.assets.precompile << ['google-analytics.js']
hope helps.
Comments
Post a Comment