“渲染:无=>”真实”返回空的纯文本文件?

发布于 2024-10-11 07:05:26 字数 986 浏览 3 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-10-18 07:05:26

从 Rails 4 开始,head 现在优先于 render :nothing1

head :ok, content_type: "text/html"

# or (equivalent)

head 200, content_type: "text/html"

优于

render nothing: true, status: :ok, content_type: "text/html"

# or (equivalent)

render nothing: true, status: 200, content_type: "text/html"

它们在技术上是相同的。如果您查看使用 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

但是,调用 head 提供了调用 render :nothing 的更明显的替代方案,因为现在明确表明您'仅生成 HTTP 标头。


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

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
温柔女人霸气范 2024-10-18 07:05:26

更新:这是旧版 Rails 版本的旧答案。对于 Rails 4+,请参阅 William Denniss 的帖子。

在我看来,响应的内容类型不正确,或者在您的浏览器中未正确解释。仔细检查您的 http 标头以查看响应的内容类型。

如果不是 text/html ,您可以尝试手动设置内容类型,如下所示:

render :nothing => true, :status => 200, :content_type => '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:

render :nothing => true, :status => 200, :content_type => 'text/html'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文