Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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
Ruby on rails 3.1 使用authenticate_或_request_与_http_basic()时接收AbstractController::DoubleRenderError_Ruby On Rails 3.1_Http Authentication_Actionview_Actioncontroller - Fatal编程技术网

Ruby on rails 3.1 使用authenticate_或_request_与_http_basic()时接收AbstractController::DoubleRenderError

Ruby on rails 3.1 使用authenticate_或_request_与_http_basic()时接收AbstractController::DoubleRenderError,ruby-on-rails-3.1,http-authentication,actionview,actioncontroller,Ruby On Rails 3.1,Http Authentication,Actionview,Actioncontroller,我有一个正常工作的控制器动作,当我在块的开头用\u http\u basic添加对authenticate\u或\u request\u的调用时,该动作会中断。以下是不起作用的操作代码: def index authenticate_or_request_with_http_basic do |username, password| username == "test1" and password == "test" end render layout: false, co

我有一个正常工作的控制器动作,当我在块的开头用\u http\u basic添加对
authenticate\u或\u request\u的调用时,该动作会中断。以下是不起作用的操作代码:

def index
  authenticate_or_request_with_http_basic do |username, password|
    username == "test1" and password == "test"
  end

  render layout: false, content_type: 'application/xml'
end
我在浏览器窗口中收到“AbstractController::DoubleRenderError”错误响应,并显示以下消息:

在此操作中多次调用渲染和/或重定向。请注意,您只能调用render或redirect,每个操作最多只能调用一次。还请注意,重定向和渲染都不会终止操作的执行,因此,如果要在重定向后退出操作,则需要执行类似“重定向到(…)并返回”的操作

当我将“authenticate\u或\u request\u with_http\u basic”逻辑放在一个单独的操作中,然后配置before\u过滤器为index操作运行它时,一切都很好。但是,除了索引之外,我没有将此逻辑用于任何操作,我想知道上面的代码为什么不起作用

解决方案

在泰勒和RKB的帮助下,我找到了解决办法。这是:

authenticated = authenticate_or_request_with_http_basic "Authentication Required" do |username, password|
  @user = User.find_by_username(username)
  @user != nil && password == "test"
end
return unless authenticated == true 

authenticate\u或\u request\u with_http\u basic
如果验证成功,则返回“true”。失败时返回401。

这听起来像是使用http进行身份验证或请求,基本是呈现或重定向。我猜当您将它移动到before_过滤器时它会工作,因为它返回false,这会取消回调链并导致第二次渲染不发生?只是猜测一下…

使用\u http\u basic调用对\u或\u request\u进行身份验证,以发送执行http basic身份验证所需的http响应(状态代码401 Unauthorized)。有关详细信息,请参阅。

在这种情况下,如果使用\u http\u basic验证\u或\u请求\u返回false,如何停止进一步执行操作?我会将使用\u http\u basic验证\u或\u请求\u调用放在before\u筛选器中。如何在单个操作中停止进一步执行?