Rails pipeline: can it be good practice to include an asset in a shared folder instead of a resource folder?

I tried a bit to add Google Analytics to my Rails 4 application (see this post ).

I have a job adding the JavaScript code that Google supplies to a new file in /public/assets/google-analytics.js. As application.html.erbI turned on:

<%= javascript_include_tag 'google-analytics' if Rails.env.production? %>

This works, but I thus did not include the JavaScript file in app/assets/javascripts/and put it in a shared folder. Therefore, the JavaScript file does not compile.

Could you say this is a good Rails practice / path?

I choose this solution because otherwise I could get it to work if I added Rails.application.config.assets.precompile += ['google-analytics.js']to config / environment / production.rb (it didn't work if I added it to the assets.rb initializer). I find this a bit more messy than the above solution.

+4
source share
1 answer

In your case, it is best to add the analytics script to the heading section views / layouts / application.html.erb

Another option would be to create a view / sharing directory with a file named _analytics.hml.erb and insert the analytics tag there and do it in your views where you need it:

<%= render 'shared/analytics' %>

, . " ", , , , .

, - "Railsy", -, www.your-site.com/analytics.js

+3

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


All Articles