Download Google Maps script only on specific pages in the Rails app

I have a Rails app with about 20 pages, and only two of them use Google Maps.

Is there a better way to exclusively load the Google Maps API - script on certain pages than this (in the head tag):

<%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false" if params[:controller] == "shops" && params[:action] == "index" %>

+4
source share
1 answer

In the main layout file, use the following code

 <%= yield :head %> 

And then in the view file for the pages you want to include in javascript, do the following:

 <% content_for :head do %> <%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false" %> <% end %> 

You can put this content_for block anywhere in your view file and it will display it in the main document where you defined yield :head

Read more here http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield and here http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method

+5
source

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


All Articles