Suppose I have a Rails app with two models Post
and Comment
. A post has_many
comments and a comment belongs_to
a post.
How can I override the respond_to
function in the show
action in order to get a JSON response containing both the Post
properties and an array of Comment
objects that it has?
Currently it is the vanilla Rails default:
# posts_controller.rb
def show
@post = current_user.posts.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end