How to do nothing in json?

I am testing the following method:

  def destroy
    if @article.destroy
      render json nothing: true, message: "removed", status: :ok
    else
      render json: @article, message: "Failed to remove", status: :bad_request
    end
  end

The line render json nothinggenerates an error

undefined `json 'method for #Api :: V1 :: ArticlesController: 0x000000074f6148

Changing the string to render json: message: "removed", status: :okdoes not matter. How to do nothing?

Update: I tried the code below, which, after uninstalling, responds No response receivedwhile I am expecting a message.

  def destroy
    if @article.destroy
      respond_to do |format|
        format.json { render nothing: true, message: "removed", status: :ok }
      end
    else
      render json: @article, message: "Failed to remove", status: :bad_request
    end
  end
+4
source share
2 answers

If you really don't want to visualize anything:

head :ok

If you want to send a JSON message in response:

render json: { message: "removed" }, status: :ok
+9
source

HTTP 204 , .

render :nothing => true, :status => 204

:

head :no_content
+5

Source: https://habr.com/ru/post/1625677/


All Articles