I use Rails 3开发者_如何学编程.1.
In my app/controllers/locations_controller.rb
I declare a @location variable
@location = Location.find(params[:id])
I've a file /app/assets/javascripts/locations.coffee.erb
where i want to use the @location
variable
myLatlng: new google.maps.LatLng(<%=@location.latitude %>, <%=@location.longitude %>)
it's not working out of the box, is there any solution to achieve that goal?
A request to a Sprockets-provided asset, or really any asset on the system, is served by an entirely separate request and so it won't have access to the instance variables set up in the controller.
What I would do is to have that JS in a partial somewhere that is rendered by the controller's view. That way it would have access to those variables.
精彩评论