This was an easy thing to accomplish in RoR… almost too easy. Update your controller’s action to:
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
end
end
The last format line is all it takes. Bring up the page with the json extension: http://localhost:3000/posts.json
[{"post":{"name":"Post1","created_at":"2011-01-29T22:54:03Z","title":"This is Post 1 title","updated_at":"2011-01-29T22:54:03Z","id":4,"content":"Post 1 content"}},{"post":{"name":"Post2","created_at":"2011-01-29T22:55:25Z","title":"This is Post 2","updated_at":"2011-01-29T22:55:25Z","id":5,"content":"This is content for post 2"}},{"post":{"name":"dfasdf","created_at":"2011-01-30T00:36:36Z","title":"dfasf","updated_at":"2011-01-30T00:36:36Z","id":8,"content":"dfsdfd"}}]
That’s it.