Simple json output on rails

I am trying to include a json object in my page that will act as a javascript object.

I have it pretty simple in my controller

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

In my opinion, I thought I could put

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

but displays

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

I expected

{"task_id": 1, "shrtDesc": "task 1"}, {"task_id": 2, "shrtDesc": "task 2"}
+5
source share
1 answer

Try:

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

: http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/

+6

All Articles