Ruby Build a Rails API Coding the API Returning JSON

Steven Fried
PRO
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.

monkeys_controller.rb
class MonkeysController < ApplicationController
  before_filter :find_monkey

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

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

2 Answers

Tim Knight
Tim Knight
28,850 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)