开发者

How to access controller's variables in app/assets/* files

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜