“render :nothing => true” returns empty plaintext file?

后端 未结 2 1284
遇见更好的自我
遇见更好的自我 2020-12-12 11:43

I\'m on Rails 2.3.3, and I need to make a link that sends a post request.

I have one that looks like this:

= link_to(\'Resend Email\', 
  {:controlle         


        
2条回答
  •  难免孤独
    2020-12-12 12:24

    Since Rails 4, head is now preferred over render :nothing.1

    head :ok, content_type: "text/html"
    
    # or (equivalent)
    
    head 200, content_type: "text/html"
    

    is preferred over

    render nothing: true, status: :ok, content_type: "text/html"
    
    # or (equivalent)
    
    render nothing: true, status: 200, content_type: "text/html"
    

    They are technically the same. If you look at the response for either using cURL, you will see:

    HTTP/1.1 200 OK
    Connection: close
    Date: Wed, 1 Oct 2014 05:25:00 GMT
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8
    X-Runtime: 0.014297
    Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
    Cache-Control: no-cache
    

    However, calling head provides a more obvious alternative to calling render :nothing because it's now explicit that you're only generating HTTP headers.


    1. http://guides.rubyonrails.org/layouts_and_rendering.html#using-head-to-build-header-only-responses

提交回复
热议问题