Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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 <;AbstractController::控制器中的DoubleRenderError_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails <;AbstractController::控制器中的DoubleRenderError

Ruby on rails <;AbstractController::控制器中的DoubleRenderError,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我遇到了这个错误,我尝试添加一个重定向到()并返回到我的access\u doc\u或\u redirect()方法,但没有成功。有什么建议吗 def access_doc_or_redirect(doc_id, msg) doc = Document.find(params[:id]) if doc.user_access?(current_user) @document = doc else flash[:alert] = msg redir

我遇到了这个错误,我尝试添加一个重定向到()并返回到我的access\u doc\u或\u redirect()方法,但没有成功。有什么建议吗

 def access_doc_or_redirect(doc_id, msg)
   doc = Document.find(params[:id])
   if doc.user_access?(current_user)
     @document = doc
   else
     flash[:alert] = msg
     redirect_to root_url and return
   end
 end

 def get
   access_doc_or_redirect(params[:id], "Sorry, no document view access.")
   redirect_to @document.file.url
 end
错误 访问文档或重定向(参数[:id],“对不起,没有文档视图访问”AbstractController::DoubleRenderError:此操作中多次调用了Render和/或redirect。请注意,您只能调用Render或redirect,并且每个操作最多只能调用一次。还要注意,redirect和Render都不会终止操作的执行,因此,如果您想在重定向后退出操作,则需要执行以下操作“将_重定向到(…)并返回”

AbstractController::DoubleRenderError:在此操作中多次调用渲染和/或重定向

错误是自描述性的,您在操作中多次调用render或redirect。让我们看看您的方法:

def access_doc_or_redirect(doc_id, msg)
  doc = Document.find(params[:id])
  if doc.user_access?(current_user)
    @document = doc  
    #this block will run fine and return @document to get method
  else
    flash[:alert] = msg
    redirect_to root_url and return
    #this block is giving you trouble because you are redirecting to root url and then control goes back to your get method where you are using redirect again and hence double render error
  end
end
修复:

如果将其用作过滤器,则要修复错误,可以执行以下操作:

def get
  #i think this is your main method so you should use redirect or render inside this method only
  #instance variables set inside access_doc_or_redirect will automatically be available inside this method
  if @document 
    redirect_to @document.file.url
  else
    flash[:alert] = "Sorry, no document view access."
    redirect_to root_url
  end
end

def access_doc_or_redirect
  @doc = Document.find(params[:id])
  if @doc.user_access?(current_user)
    @document = doc
  end
end

谢谢,但问题是我试图保持代码干燥,并将访问\u doc\u或\u重定向作为before\u过滤器。为什么“and return”不起作用?@user2012677正如我在回答中使用return时所解释的,它将从访问中返回\u doc\u或\u redirect方法,控制将转到get方法,而您在那里再次使用redirect,因此您的错误我如何重定向和“结束”那么?@user2012677更新了我的答案,我认为这是你能做的最好的了,因为get方法是你的案例中的主要方法,而另一个方法只是一个过滤器,所以在get方法中重定向是有意义的