Passing paramters from rails views to jquery (gmap)

眉间皱痕 提交于 2020-01-03 03:33:09

问题


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

It seems very easy to use:

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

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

something like <%= show_map ("47.66","9.56","6") %>

or more harcoded

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

回答1:


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

In your app/helpers/controllername_helper.rb file:

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

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>


来源:https://stackoverflow.com/questions/3666739/passing-paramters-from-rails-views-to-jquery-gmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!