Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Rails API Coding the API Response Codes

By adding if banana.safe head 200 end the previous step throws an error saying i need to declare create.

Been scratching my head on this one for a while, but i can't seem to figure it out.

bananas_controller.rb
class Api::BananasController < ApplicationController

  def create
    banana = Banana.new(banana_params)
    if banana.safe
      head 200
    end
  end

  private

  def banana_params
    params.require("banana").permit("name")
  end
end

3 Answers

try

  def create
    @banana = Banana.new(banana_params)
    if @banana.save
      head 200
    end
  end

I forgot to mention earlier that banana is supposed be an instance variable.

Hi, Daan Oosting

create method does not need this block of code

    if banana.safe
      head 200
    end

Hi William, that's correct, but in the next step they ask you to add this to the code. And all new code is added to previous steps. So i get an error in a step i have already completed.

sorry, about that, I was thinking that you're only referring to part 1, let me check for you.

Yes! That did it! It's weird that they do not cover it like that in the videos. Thanks William, enjoy your Sunday.

no problem, Daan. And I'm sure once you passed part 2, part 3 should be a piece of cake, right? Enjoy your weekend too

Absolutely! :)