Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple json output in rails

I'm trying to include in my page, a json object which will act as a javascript object.

I've got it pretty simply in my controller

   def index
   @tasks = User.select("task_id,desc")
   end

in my view, I thought I'd be able to put

 var tasks = <%= @tasks.as_json %>

but that outputs

[#<Task task_id: 1, shrtDesc: "task 1">, #<Task task_id: 2, shrtDesc: "task 2">]

what I was expecting was

{"task_id":1, "shrtDesc": "task 1"}, {"task_id":2,"shrtDesc":"task 2"}
like image 653
pedalpete Avatar asked Feb 11 '11 23:02

pedalpete


1 Answers

Try:

 var tasks = <%= @tasks.to_json %>

Here are some explanations: http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/

like image 149
apneadiving Avatar answered Oct 13 '22 00:10

apneadiving