I am new to rails and am confused about the format.json
lines in the code below. What do status: :created
and location: @product
refer to?
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render json: @product, status: :created, location: @product }
format.js
else
format.html { render action: "new" }
format.json { render json: @product.errors, status: :unprocessable_entity }
format.js
end
end
end
Is including status and location optional? I'm mostly confused about what is optional and why one might add a custom status/location.