In using the rails 5.1.4 scaffolding for controllers I see that the default approach to deal with a save failure in the #create
method is to render #new
again (with a status of 200
).
respond_to do |format|
if @company.save
format.html { redirect_to @company, notice: 'Company was successfully created.' }
format.json { render :show, status: :created, location: @company }
else
format.html { render :new }
format.json { render json: @company.errors, status: :unprocessable_entity }
end
end
Is there some good reason why the HTML response doesn't render 422
like the JSON version?
The reason this is a problem is that it makes testing the response code difficult in integration tests (i.e. validation error or not the #create
method is going to return 200
).