Search code examples
ruby-on-railsrestlink-to

"render :nothing => true" returns empty plaintext file?


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?


Solution

  • 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'