Ruby Build a Rails API Coding the API Response Codes

Daan Oosting
Daan Oosting
2,203 Points

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

William Li
PRO
William Li
Pro Student 26,795 Points

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.

William Li
PRO
William Li
Pro Student 26,795 Points

Hi, Daan Oosting

create method does not need this block of code

    if banana.safe
      head 200
    end
Daan Oosting
Daan Oosting
2,203 Points

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.

William Li
William Li
Pro Student 26,795 Points

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

Daan Oosting
Daan Oosting
2,203 Points

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

William Li
William Li
Pro Student 26,795 Points

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