Ruby On Rails | What is format.html/json for?

Viewed 7212

So I have been going through Rails Zombies and have gotten to the part explaining format.html and .json My question is what do these lines of code do, and why do we have them? If I write these methods or actions without these format codes they work perfectly fine, as i'd assume they simply display in html format by default? If somebody could clear up exactly what this code does I'd be grateful, I also do not fully understand what JSON is.

def create
    @zombie = Zombie.new(zombie_params)

    respond_to do |format|
      if @zombie.save
        format.html { redirect_to @zombie, notice: 'Zombie was successfully created.' }
        format.json { render :show, status: :created, location: @zombie }
      else
        format.html { render :new }
        format.json { render json: @zombie.errors, status: :unprocessable_entity }
      end
    end
1 Answers
Related