Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Rails控制器方法在Ajax调用后不重新加载页面_Jquery_Ruby On Rails_Ajax_Authentication_Render - Fatal编程技术网

Jquery Rails控制器方法在Ajax调用后不重新加载页面

Jquery Rails控制器方法在Ajax调用后不重新加载页面,jquery,ruby-on-rails,ajax,authentication,render,Jquery,Ruby On Rails,Ajax,Authentication,Render,因此,我构建了一个身份验证方法,它充当控制器中其他方法的前置过滤器。基本上,如果用户未登录,我希望该方法重定向到根路径。这是我的用户!过滤方法之前: def authenticate_user! unless current_user flash[:notice] = "Your session has ended. Please login again." render js: "window.location.pathname = '#{root_

因此,我构建了一个身份验证方法,它充当控制器中其他方法的前置过滤器。基本上,如果用户未登录,我希望该方法重定向到根路径。这是我的用户!过滤方法之前:

def authenticate_user!
      unless current_user
        flash[:notice] = "Your session has ended. Please login again."
        render js: "window.location.pathname = '#{root_path}'"
      end
end
我遇到的问题是,窗口本身并没有重新加载,而是页面中的内容元素,并显示文本:“window.location.pathname='/'。我无法使用重定向到(我找到了),因为它似乎只将某个内容元素发送回根路径(而像navbar这样的元素保持不变)。这就是为什么我一直在寻找一个完整的窗口重新加载。任何帮助都将不胜感激

使用服务器日志编辑

Started GET "/projects/editHeader?fileName=test.csv" for 127.0.0.1 at 2014-07-04 10:37:21 -0400
Processing by ProjectsController#editHeader as TEXT
  Parameters: {"fileName"=>"test.csv"}
Filter chain halted as :authenticate_user! rendered or redirected
Completed 200 OK in 2ms (Views: 0.1ms | ActiveRecord: 0.0ms)


Started GET "/projects/headerDataPreview?fileId=NaN" for 127.0.0.1 at 2014-07-04 10:37:21 -0400
Processing by ProjectsController#headerDataPreview as TEXT
  Parameters: {"fileId"=>"NaN"}
Filter chain halted as :authenticate_user! rendered or redirected
Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.0ms)

对于ajax请求,您需要在before_filter方法中指定请求类型

更改您的
authenticate\u用户方法到

def authenticate_user
  unless current_user
    if request.xhr? # if its a ajax request then redirect with javascript
      flash.keep[:notice] = 'Bla bla bla' # keeps the flash message for next request
      render :js => "window.location = '#{root_path}'"
    else
      flash[:notice] = "Bla bla bla"
      redirect_to root_path
    end
  end
end

首先,把js放在控制器中是不好的做法。不要将客户端脚本与服务器处理的控制器混合使用。关于您的问题,为什么不直接使用rails重定向呢

def authenticate_user!
  unless current_user
    redirect_to root_path, :notice => "Your session has ended. Please login again."
  end
end

重定向到的问题是整个屏幕没有改变——只有内部内容元素。因此,即使我希望用户在没有导航栏的情况下进入登录初始页面,导航栏仍会保持在屏幕上;它只是将屏幕显示为一个空白页面,文本为“window.location='\'。您可以发布您的服务器日志吗?在我当前的项目中,我使用了相同的代码,而且效果很好。对
render:js=>
的巨大感激应该标记为正确