4

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"}

1 Answer 1

6

Try:

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

aaarrrggghhh... thanks apneadiving! didn't know about to_json, everything I was seeing said as_json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.