开发者

AbstractController::DoubleRenderError that shouldn't be

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?


Try adding " and return" after the respond_with lines:

respond_with head :ok and return 

respond_with({:error=>'unauthorised'}, :status => :forbidden) and return


head(:ok) doesn't return something you can respond_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 calls render, respond_with calls render, hence the double render error.

You should change that line to just head :ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜