Passing paramters from rails views to jquery (gmap)
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 });
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>
精彩评论