将参数从Rails视图传递到jquery(gmap) [英] Passing paramters from rails views to jquery (gmap)

查看:93
本文介绍了将参数从Rails视图传递到jquery(gmap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的rails3应用程序中使用此jquery插件 http://gmap.nurtext.de/(适用于jQuery的Google Maps插件)

I want to use this jquery plugin in my rails3 apps http://gmap.nurtext.de/ (Google Maps Plugin for jQuery)

它似乎很容易使用:

$("#map").gMap({ markers: [{ latitude: 47.660937,
                              longitude: 9.569803,
                              zoom: 6 });

但是我有一个问题,我如何将值传递给这个jasvascript代码?

But i have a question, how i pass the values to this jasvascript code?

类似<%= show_map ("47.66","9.56","6") %>

或更多受骚扰

$("#map").gMap({ markers: [{ latitude: variable1,
                              longitude: variable2,
                              zoom: variable3 });

推荐答案

您需要创建一个辅助函数,该函数使用您指定的参数返回包含地图JavaScript的字符串.

You need to create a helper function that returns a string containing the map JavaScript using the parameters you specify.

在您的 app/helpers/控制器名称 _helper.rb 文件中:

In your app/helpers/controllername_helper.rb file:

def show_map(lat, long, zoom)
  "$('#map').gMap({ markers: [{ latitude: #{lat}, longitude: #{long}, zoom: #{zoom} });"
end

然后在您的视图中 app/views/控制器名称/动作名称 ,使用您在问题中引用的相同代码进行调用:

Then in your view app/views/controllername/actionname, call it using the same code you referenced in your question:

<script type="text/javascript">
  <%= show_map ("47.66","9.56","6") %>
</script>

这篇关于将参数从Rails视图传递到jquery(gmap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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