我想在浏览器中通过地址http://localhost:3000/test 返回在浏览器中[{"firstname":"1","lastname":"3"},{"firstname":"2","lastname":"4",}]这样的json数据(jquery easyui的DataGrid中url参数需要这样的json值:url="http://localhost:3000/test")。
在TestController中:
class TestController < ApplicationController
def index
@users=User.find(:all)
??
end
end
(其中表user只有2条记录,firstname、lastname是表user的字段,"1""2""3""4"是这两个字段的值)
请问:?? 处怎么写?
[code="ruby"]
respond_to do |format|
format.json{
render :inline => @users.to_json
}
[/code]
[{"firstname":"1","lastname":"3"},{"firstname":"2","lastname":"4",}] 这应该是javascript 数组了,而不是JSON
[code="ruby"]
class User
def to_j
%Q({"firstname":"#{firstname}","lastname":"{lastname}"})
end
end
[/code]
???写上下面段代码
render_text "[#{@users.map{|u|u.to_j}.join(',')}]"
render :json=> @userto_json
先用字符串来构成json数据,然后再用render
json_str = "{'xx': '1', 's':'2'}"
在render json=>json_str, status=>"200 ok"