Rails 5 - & ldquo; Render and / or redirection were called several times in this action & rdquo;

advertisements

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?


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

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