我有一个带索引的注释控制器,并创建其他操作。这两个动作都响应html和js格式。
每当我通过ajax获得创建请求时,我想添加新注释然后重定向到index.js,因此屏幕上的注释会在不重新加载页面的情况下更新。
这种事情在Chrome中很有效。但是每当我在Firefox或IE中尝试这个时,事实证明,create.js的重定向落在index.html ...
即使我强制重定向为js:
redirect_to polymorphic_path([@commentable, :comments]), :format => 'js'
它在Firefox和IE中的format.html中出现。
知道这里可能会发生什么吗?
答案 0 :(得分:1)
浏览器以不同方式处理302请求的方式存在各种问题。一些丢失请求类型,另一些丢失请求方法(示例票证:http://trac.tools.ietf.org/wg/httpbis/trac/ticket/160)。
我建议在使用JS时,不要重定向到新的URL,而只是渲染相同的动作。所以像这样:
class CommentsController < ApplicationController
def index
setup_for_index
respond_to :html, :js
end
def create
# Creation stuff...
respond_to do |format|
format.html {redirect_to :action => :index}
format.js do
setup_for_index
render :action => :index
end
end
end
private
def setup_for_index
@comments = ...
end
end