Ruby Build a Rails API Coding the API Returning JSON

Steven Fried
Steven Fried
Pro Student 5,638 Points

What is the correct answer to the code challenge for the as_json lesson (nesting)?

I have tried multiple times to get the answer to this code challenge, watched the video again and again, and I have no idea what I'm doing wrong. It would be helpful to have ready access to the correct answers so I can check myself without having to ask a question. If those are available, it is unclear where to find them.

class MonkeysController < ApplicationController
  before_filter :find_monkey

  def show
    render json: @monkey.as_json(include::bananas)

  def find_monkey
    @monkey = Monkey.find(params[:id])

2 Answers

Tim Knight
Tim Knight
Treehouse Moderator 28,846 Points

Hi Steven,

You're really close. Includes is the attribute you're passing item(s) into. When passing in a symbol it's more common to use the older "hashrocket" syntax.

So instead of include::bananas which makes it look like you're trying to call a class on a module, you'd do something like this [attribute] => [symbol].

So putting that together:

render json: @monkey.as_json(:include => :bananas)