Ruby-on-rails "render :nothing => true" 返回空的纯文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4632271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"render :nothing => true" returns empty plaintext file?
提问by user225643
I'm on Rails 2.3.3, and I need to make a link that sends a post request.
我在 Rails 2.3.3 上,我需要创建一个发送 post 请求的链接。
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:
这使得链接上的 JavaScript 行为适当:
<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."
但是当我单击该链接时,我的浏览器会下载一个名为“resend_confirm_email”的空文本文件。
What gives?
是什么赋予了?
回答by William Denniss
Since Rails 4, headis now preferred over render :nothing.1
由于 Rails 4,head现在优先于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:
它们在技术上是相同的。如果您查看使用 cURL 的响应,您将看到:
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 headprovides a more obvious alternative to calling render :nothingbecause it's now explicit that you're only generating HTTP headers.
但是,调用head提供了一种更明显的调用替代方法,render :nothing因为现在明确表示您只生成 HTTP 标头。
回答by vonconrad
UPDATE: This is an old answer for legacy Rails versions. For Rails 4+, see William Denniss' post below.
更新:这是旧 Rails 版本的旧答案。对于 Rails 4+,请参阅下面的 William Denniss 帖子。
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.
在我看来,响应的内容类型不正确,或者在您的浏览器中没有正确解释。仔细检查您的 http 标头以查看响应的内容类型。
If it's anything other than text/html, you can try to manually set the content type like this:
如果不是text/html,您可以尝试手动设置内容类型,如下所示:
render :nothing => true, :status => 200, :content_type => 'text/html'

