3
votes

After updating to Rails 5.0, I'm getting the following error:

"AbstractController::DoubleRenderError in RegistrationsController#create

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 "reditect_to(...) and return"."

This is my code, which worked before my update:

def create
  # save record
  if params[:stuff].nil?
    respond_to do |format|
      format.js
    end
  else
    redirect_to root_path
  end
end

I've tried a lot of different syntaxes, for example:

redirect_to(root_path) and return

redirect_to(root_path)
return

return and redirect_to(root_path)

return redirect_to(root_path)

But everything returns the same error. Anyone know the proper syntax?

2
Do you have any other redirect_to or render in your action? Give this a try: return redirect_to(root_path).Gerry
@Gerry I added the full action. That code doesn't work either.Joe Morano
Since no other redirect_to or render is present, i will also suggest using byebug to locate where your code is calling that second render.Gerry

2 Answers

2
votes

You probably have render or redirect where you're showing the # save-record.

Try this:

  • Add gem byebug to your Gemfile if you don't already have it installed, run bundle to install it, and restart Rails
  • Add byebug to the start of your create method
  • Invoke create from the browser or command-line, and step through it with n and s to step into other functions. You'll probably notice render or redirect is being called twice
1
votes

You can use performed? to test for, or debug, a double render/redirect.