133

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', 
  {:controller => 'account', :action => 'resend_confirm_email'}, 
  {:method => :post} )

Which makes the appropriate JavaScript behavior on the link:

<a href="/account/resend_confirm_email" 
  onclick="var f = document.createElement('form'); 
  f.style.display = 'none'; 
  this.parentNode.appendChild(f); 
  f.method = 'POST'; 
  f.action = this.href;
  var s = document.createElement('input'); 
  s.setAttribute('type', 'hidden'); 
  s.setAttribute('name', 'authenticity_token'); 
  s.setAttribute('value', 'EL9GYgLL6kdT/eIAzBritmB2OVZEXGRytPv3lcCdGhs=');
  f.appendChild(s);
  f.submit();
  return false;">Resend Email</a>'

My controller action is working, and set to render nothing:

respond_to do |format|
  format.all { render :nothing => true, :status => 200 }
end

But when I click the link, my browser downloads an empty text file named "resend_confirm_email."

What gives?

1

2 Answers 2

289

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
3
  • Since this also works in Rails 3, it should be the preferred solution for that too (but obviously the OP is on a Rails 2.3 app so the chosen answer was appropriate). Commented Sep 17, 2014 at 18:06
  • 2
    head 200 results in a 304 response for me (on rails 4.1.6). The console shows 200 status code, but chrome (network panel) shows 304. The render :nothing => true approach worked. Commented Oct 29, 2014 at 12:57
  • 2
    Is content type necessary if only a header is being returned?
    – Usagi
    Commented Sep 22, 2016 at 17:48
149

UPDATE: This is an old answer for legacy Rails versions. For Rails 4+, see William Denniss' post.

Sounds to me like the content type of the response isn't correct, or isn't correctly interpreted in your browser. Double check your http headers to see what content type the response is.

If it's anything other than text/html, you can try to manually set the content type like this:

render :nothing => true, :status => 200, :content_type => 'text/html'
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.