开发者

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

开发者 https://www.devze.com 2023-03-15 18:15 出处:网络
I use Rails 3开发者_如何学编程.1. In my app/controllers/locations_controller.rb I declare a @location variable

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

精彩评论

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