【发布时间】:2017-04-08 03:26:05
【问题描述】:
更新到 Rails 5.0 后,我收到以下错误:
"AbstractController::DoubleRenderError in RegistrationsController#create
在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且每个操作最多调用一次。另请注意,无论是重定向还是渲染都不会终止该操作的执行,因此如果您想在重定向后退出某个操作,则需要执行“reditect_to(...) and return”之类的操作。”
这是我的代码,在我更新之前可以使用:
def create
# save record
if params[:stuff].nil?
respond_to do |format|
format.js
end
else
redirect_to root_path
end
end
我尝试了很多不同的语法,例如:
redirect_to(root_path) and return
redirect_to(root_path)
return
return and redirect_to(root_path)
return redirect_to(root_path)
但一切都返回相同的错误。有人知道正确的语法吗?
【问题讨论】:
-
您还有其他
redirect_to或render在您的操作中吗?试试看:return redirect_to(root_path). -
@Gerry 我添加了完整的操作。该代码也不起作用。
-
由于没有其他
redirect_to或render存在,我还建议使用byebug来定位您的代码调用第二次渲染的位置。
标签: ruby-on-rails ruby redirect controller ruby-on-rails-5