How to return HTTP 204 in a Rails controller

后端 未结 3 1226
北恋
北恋 2021-02-01 12:31

This seems to be basic but I\'m a Ruby/Rails beginner. I need to simply return HTTP 204 in a controller. Would

respond_to do |format|
  format.html  
end


        
相关标签:
3条回答
  • 2021-02-01 12:37
    head :no_content
    

    Tested with Rails 3.2.x, 4.x. It causes the controller method to respond with the 204 No Content HTTP status code.

    An example of using this inside a controller method named foobar:

    def foobar
      head :no_content
    end
    
    0 讨论(0)
  • 2021-02-01 12:44

    If you don't want to render anything at all you can do this:

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

    or like this:

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

    Or you can use the :status => 204 part with any other render command

    0 讨论(0)
  • 2021-02-01 12:57

    Look at the head method:

    Return a response that has no content (merely headers). The options argument is interpreted to be a hash of header names and values.

    0 讨论(0)
提交回复
热议问题