How to access variable sent from Rails 4 controller in JavaScript? -
i have @coords
variable:
#maps controller def index @coords = map.all end
inside index.html.erb, have:
<%= content_tag 'div', id: 'coords', data: @coords.to_json %> <% end %>
how content_tag looks after rendering html page:
[{"id":1,"vibration_level":456,"lat":"71.45543","lon":"53.43424","time_sent":"1994-05-20t00:00:00.000z","created_at":"2015-06-13t06:53:30.789z","updated_at":"2015-06-13t06:53:30.789z"} ]
how can access javascript file.
you should consider using gon gem. gem sending data rails javascript.
you can smth this:
#map controller def index @coords = map.all gon.coords = @coords.to_json end
and can acces variable in js: gon.coords
Comments
Post a Comment