开发者

simple json output in rails

开发者 https://www.devze.com 2023-02-10 14:30 出处:网络
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

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消