In this episode, we're focusing on listing the discussion posts inside our forum app. We start out by thinking about how best to fetch and show all the posts for a discussion, especially when there could be loads of them. Instead of just attaching every post to the main discussion API resource (which doesn't work well with pagination), we decide to fetch and paginate the posts separately from the discussion resource.
We walk through creating a PostResource
to shape the data for each post, and we set up our controller to return a paginated collection of posts for a given discussion. This way, we can keep things fast and tidy if there are hundreds (or thousands!) of replies.
On the frontend, we create a super simple Post
component that displays each post's ID at first (just to make sure it's all working). We then update it to actually render the post body, and borrow a few styles from our discussion component so everything looks consistent. We also add some styling tweaks to make sure posts are nicely grouped and separated from each other.
By the end of this video, you’ll see all the posts for a discussion showing up, neatly paginated and styled. Next up, we’ll display extra info like who posted each reply and when it was posted!