2

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).

krsyoung
  • 1,171
  • 1
  • 12
  • 24

1 Answers1

2

Most likely the reason is that historically, most browsers don't tend to understand a lot of the HTTP codes - they only deal with some very simple ones. I think they mostly just discard ones they don't understand.

But there's no reason why you can't change create to send the most appropriate HTTP response-code and start making the web a better place one website at a time :)

Taryn East
  • 27,486
  • 9
  • 86
  • 108