“渲染:无=>”真实”返回空的纯文本文件?
我使用的是 Rails 2.3.3,我需要创建一个发送发布请求的链接。
我有一个看起来像这样的:
= link_to('Resend Email',
{:controller => 'account', :action => 'resend_confirm_email'},
{:method => :post} )
这使得链接上有适当的 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>'
我的控制器操作正在工作,并且设置为不渲染任何内容:
respond_to do |format|
format.all { render :nothing => true, :status => 200 }
end
但是当我单击链接时,我的浏览器会下载一个名为“resend_confirm_email”的空文本文件。
什么给?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Rails 4 开始,
head
现在优先于render :nothing
。1优于
它们在技术上是相同的。如果您查看使用 cURL 的响应,您将看到:
但是,调用
head
提供了调用render :nothing
的更明显的替代方案,因为现在明确表明您'仅生成 HTTP 标头。Since Rails 4,
head
is now preferred overrender :nothing
.1is preferred over
They are technically the same. If you look at the response for either using cURL, you will see:
However, calling
head
provides a more obvious alternative to callingrender :nothing
because it's now explicit that you're only generating HTTP headers.更新:这是旧版 Rails 版本的旧答案。对于 Rails 4+,请参阅 William Denniss 的帖子。
在我看来,响应的内容类型不正确,或者在您的浏览器中未正确解释。仔细检查您的 http 标头以查看响应的内容类型。
如果不是
text/html
,您可以尝试手动设置内容类型,如下所示: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: