我在Rails 2.3.3上,我需要创建一个发送帖子请求的链接.
我有一个看起来像这样的:
= link_to('Resend Email',
{:controller => 'account', :action => 'resend_confirm_email'},
{:method => :post} )
Run Code Online (Sandbox Code Playgroud)
这会在链接上产生适当的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>'
Run Code Online (Sandbox Code Playgroud)
我的控制器操作正在运行,并设置为不渲染:
respond_to do |format|
format.all { render :nothing => true, :status => 200 }
end
Run Code Online (Sandbox Code Playgroud)
但是当我点击链接时,我的浏览器会下载一个名为"resend_confirm_email"的空文本文件.
是什么赋予了?
这个代码在rails 5中
class PagesController < ApplicationController
def action
render nothing: true
end
end
Run Code Online (Sandbox Code Playgroud)
导致以下弃用警告
DEPRECATION WARNING: :nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body.
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
可能这样的问题不是新的,但我没有发现任何类似的东西.我有这样的jQuery代码:
$.ajax({
url : ('/people/'+id),
type : 'DELETE',
dataType : 'json',
success : function(e) {
table.getItems(currentPage);
}
});
Run Code Online (Sandbox Code Playgroud)
我的Rails控制器看起来像这样:
def destroy
@person = Person.find(params[:id])
@person.destroy
respond_to do |format|
format.html { redirect_to(people_url) }
format.json { render :json => @person, :status => :ok }
end
end
Run Code Online (Sandbox Code Playgroud)
这很有效.
但是当我使用以下内容(由标准生成)时,success不会调用回调:
def destroy
@person = Person.find(params[:id])
@person.destroy
respond_to do |format|
format.html { redirect_to(people_url) }
format.json { head :ok }
end
end
Run Code Online (Sandbox Code Playgroud)
测试下rails 3.0.3,jQuery 1.4.2和Firefox 3.6.13.
Firebug说,在这两种情况下都会触发查询并返回200 OK,在这两种情况下都会删除项目.但在第二种情况下,不会调用回调. …