d2kagw
d2kagw

Reputation: 807

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?

Upvotes: 3

Views: 6020

Answers (2)

Miles Georgi
Miles Georgi

Reputation: 51

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.

Upvotes: 5

Yule
Yule

Reputation: 9764

Try adding " and return" after the respond_with lines:

respond_with head :ok and return 

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

Upvotes: 5

Related Questions