It seems to_json is returning an incorrect format.I have a Product model with id , title, description. In ProductsController, in index I have
def index @products = Product.all
respond_to do |format| format.html # index.html.erb format.xml { render :xml => @products.to_xml(:procs=>allprocs, :skip_instruct => true,:skip_types=>true,:except=>[:id,:created_at,:updated_at] ) } format.json { render :json => {:products=>@products.to_json(:only=>[:id,:title]) } }
end end
It returns the result as
{"products":"[{\"product\":{\"title\":\"Cross Stitch Design\",\"id\": 4}},{\"product\":{\"title\":\"Effective Java\",\"id\":20}},{\"product \":{\"title\":\"Thinking in Java\",\"id\":22}},{\"product\":{\"title\": \"Programming in Java\",\"id\":23}},{\"product\":{\"title\":\"My experiment\",\"id\":28}}]"}
It is escaping " is OK. But "products":"[{ ... the javascript treats it as a string because of ". can you please suggest any solution to this problem.