Javascript 使用format.js重定向和呈现通知

Javascript 使用format.js重定向和呈现通知,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,我遇到了一种情况,我不确定事情是否可以处理得更好 当使用remotipart通过ajax调用上载文件时,所有操作都由js执行 Processing by DocumentsController#create as JS 我无法使用js响应使页面重定向,但是我在这里找到了一个解决方案 if @document.save format.html { redirect_to root_path, notice: success_save_msg } format.js { ren

我遇到了一种情况,我不确定事情是否可以处理得更好

当使用remotipart通过ajax调用上载文件时,所有操作都由js执行

Processing by DocumentsController#create as JS
我无法使用js响应使页面重定向,但是我在这里找到了一个解决方案

if @document.save
     format.html { redirect_to root_path, notice: success_save_msg }
     format.js { render :js => "window.location.href='"+root_path+"'" }
   else
     format.html
     format.js { render action: 'create.js.erb' }
   end
因此页面确实重定向了,但我不再像呈现html那样收到通知

有没有更好的方法来处理这个问题,或者用我当前的实现来显示通知

更新 到目前为止,我已经做到了这一点(本质上与我所知道的相同,但认为重定向路径在控制器中没有硬编码,因此更简洁)

redirect.js.erb

window.location.replace("<%= root_path %>");
window.location.replace("<%= root_path %>");
def create
@document = current_user.documents.new(document_params)
respond_to do |format|

   if @document.save
     format.html { redirect_to root_path, notice: success_save_msg }
     format.js { flash[:notice] = 'File Successfully Uploaded'
                 render action: 'redirect.js.erb'
               }
   else
     format.html
     format.js { render action: 'create.js.erb' }
   end

end