Rails 5.1 how to render no content response in format.json

Viewed 5175

I have looked at at least 10 questions on this and tried everything (eg. like this question), but none of the suggestions work, eg:

This for example:

format.json head :no_content and return

throws this error:

ArgumentError (wrong number of arguments (given 2, expected 1)):

Whilst this:

format.json head and return

throws this error:

ArgumentError (wrong number of arguments (given 0, expected 1..2)):

This is what I currently have in the controller action:

def paid
    @user = User.find_by(id: session['user_id'])
    respond_to do |format|    
      if !@user.nil?
        @user.is_paid = true
        @user.payment_reference = payment_params[:reference]
        @user.save
        format.json { render head, status: :no_content } and return
      else
        format.json render json: { message: "failed to record payment" }, status: :unprocessable_entity
      end
    end
  end

This works but in the console throw an error:

No template found for PaymentsController#paid, rendering head :no_content

I don't want to add an empty template to solve this. I want to tell Rails that I want to render head :no_content!

This question shows that in Rails 3.1 the default generated code did this:

format.json { head :no_content }

but that shows the same exact error in the log.

1 Answers
Related