I want to convert an array of Place objects to json, I did it like this:
var places = <%= @places.to_json.html_safe %>;
The only problem is that every place in the @places array has a linked tag list that is not included. I use the action_as_taggable_on gem to process tags, so to get a list of tags for a place, I need to specify place.tag_list.
What do I need to do to enable tag_list for every place in the javascript array? I think I will need to write my own to_json method, but I do not know how to do this.
EDIT
It turns out it's easier than I understood. I could say the following:
var places = <%= @places.to_json(:include => :tags).html_safe %>
The only problem is that this includes more information about each tag than I really need. Each tag has an id and a name that I really want is just a list with the tag names in it.
json javascript ruby-on-rails-3
lashleigh
source share