仅在Rails应用中的某些页面上加载Google地图脚本 [英] Load Google Maps script only on certain pages in Rails app

查看:83
本文介绍了仅在Rails应用中的某些页面上加载Google地图脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由大约20页组成的Rails应用程序,只有我使用Google地图的两个应用程序。



有没有更好的方法可以在某些页面上专门加载Google Maps API脚本(比如头标记):

<%= javascript_include_taghttps://maps.googleapis.com/maps/api/js?v=3.exp&sensor=falseif params [:controller] ==商店&& params [:action] ==index%>

解决方案

布局文件使用以下代码:

 <%= yield:head%> 

然后在包含javascript的页面的视图文件中执行此操作;

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

你可以将这个content_for块放在你的视图文件的任何位置,并且它会在文档头部您已在中定义了yield:head



更多信息http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield ,这里http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method


I have a Rails application that consists of about 20 pages and only on two of those I'm using Google Maps.

Is there any 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" %>

解决方案

In the head of you layout file use the following code

<%= yield :head %>

And then in the view file for the pages you want to include the javascript do this;

<% 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 render in the head of the doc where you have defined the yield :head

More info 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

这篇关于仅在Rails应用中的某些页面上加载Google地图脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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