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开发者_StackOverflow社区"}
Try:
var tasks = <%= @tasks.to_json %>
Here are some explanations: http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/
精彩评论