3

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?

JJD
  • 50,076
  • 60
  • 203
  • 339
d2kagw
  • 797
  • 1
  • 7
  • 26

2 Answers2

5

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Miles Georgi
  • 51
  • 1
  • 1
5

Try adding " and return" after the respond_with lines:

respond_with head :ok and return 

respond_with({:error=>'unauthorised'}, :status => :forbidden) and return
Yule
  • 9,668
  • 3
  • 51
  • 72
  • I've added the 'and return' to the controller, but I'm still getting that same error. – d2kagw Mar 24 '11 at 20:43
  • I don't understand why, but if I change it to one of the *older* `respond_to` blocks the error goes away. – d2kagw Mar 24 '11 at 21:00