2

I am following this really great tutorial. Now I would like to get the comments for a specific posting as JSON output.

Doing this for the posting is easy because it is just to call the .json. But how does it work with the comments?

3
  • Do you want the comments along with the posting, or each individual comment with it's own json responding route? Commented Sep 22, 2011 at 12:44
  • At least I would like both :-) The primary goal is to see the comments with it's own json responding route. Later on I would like to have the other way also. Commented Sep 22, 2011 at 12:49
  • I posted an answer, which does not include a great deal of actual code but should help you get things going in the right direction. Commented Sep 22, 2011 at 12:58

1 Answer 1

1

So this is mostly sans-code, but the ideas should at least set you in the right direction.

To provide comments with their own route, first set up a route for comments in your routes.rb file, then create a show method in the comments controller (similar to the way you set it up for a posting). In the comments controller show method, just render the comment the same way you do for the posting.

It is a bit easier to just include comments along with the posting when requesting the json output. In the posts controller show method, use render :json => @posting.to_json(:include => :comment) (substitute @posting for the name of the Post object you find in the show method).

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

Comments

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.