简体   繁体   中英

Rails 5 - “Render and/or redirect were called multiple times in this action”

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 # 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM