AbstractController::DoubleRenderError 不应该的
当我在用户控制器中点击此销毁方法时,出现以下错误。
AbstractController::DoubleRenderError(在此操作中多次调用渲染和/或重定向。 请注意,您只能 调用渲染或重定向,每个操作最多调用一次。另请注意 重定向和渲染都不会终止操作的执行,所以如果 你想在重定向后退出一个动作,你需要做一些事情 就像“redirect_to(...) 并返回”。):
这是一个奇怪的问题,因为老实说我只响应一次调用。
这是我的行动:
def destroy
user = User.find(params[:id])
if user.has_role_for? current_client
# then we remove the role
user.has_no_roles_for! current_client
# was that the users only role?
if user.roles.count == 0
user.destroy
end
respond_with head :ok
else
respond_with({:error=>'unauthorised'}, :status => :forbidden)
end
end
有什么想法吗?
I've been getting the following error when I hit this destroy method in a my User controller.
AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action.
Please note that you may only
call render OR redirect, and at most once per action. Also note that
neither redirect nor render terminate execution of the action, so if
you want to exit an action after redirecting, you need to do something
like "redirect_to(...) and return".):
It's a strange one, because I honestly am only responding once to the call.
Here's my action:
def destroy
user = User.find(params[:id])
if user.has_role_for? current_client
# then we remove the role
user.has_no_roles_for! current_client
# was that the users only role?
if user.roles.count == 0
user.destroy
end
respond_with head :ok
else
respond_with({:error=>'unauthorised'}, :status => :forbidden)
end
end
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在respond_with行后添加“并返回”:
Try adding " and return" after the respond_with lines:
head(:ok)
不会返回您可以respond_with
的内容。head :ok
渲染没有主体的 200。respond_with
通过响应者呈现您传递给它的对象的一些表示。head
调用render
,respond_with
调用render
,因此出现双重渲染错误。您应该将该行更改为
head :ok
。head(:ok)
doesn't return something you canrespond_with
.head :ok
renders a 200 with no body.respond_with
renders via the responder some representation of the object you passed into it.head
callsrender
,respond_with
callsrender
, hence the double render error.You should change that line to just
head :ok
.