为什么Rails应该渲染422时渲染200?

这是我的控制器动作

  def create
    @book = Book.new(title: params[:title])
    if @book.save
      render json: @book.to_json
    else
      render json: {:errors => @book.errors.to_json}, status: 422
    end
  end

这是我的React代码

handleSubmit = (event) => {
    event.preventDefault();
    console.log('here')
    Axios.post(
        `${process.env.REACT_APP_API_URL}/books`,
        {
            title: this.state.title
        }
    ).then(response => {
        console.log(response);
    }).catch(error => {
        console.log(error)
    })
}

react代码中的正常响应块就是所谓的。我看一下浏览器的JavaScript控制台,响应如下所示:

{data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}


这是我在rails控制台输出中看到的内容:

Started POST "/books" for 10.0.0.204 at 2020-05-18 07:59:46 +0000
Processing by BooksController#create as HTML
  Parameters: {"title"=>"asdf", "book"=>{"title"=>"asdf"}}
   (0.2ms)  BEGIN
  ↳ app/controllers/books_controller.rb:11
   (0.1ms)  ROLLBACK
  ↳ app/controllers/books_controller.rb:11
error
Completed 200 OK in 4ms (Views: 0.1ms | ActiveRecord: 0.3ms)

因此出于某种原因,它仍将状态设置为200。

评论