【发布时间】:2017-07-09 00:57:46
【问题描述】:
我有一个小问题,这段代码有效:
def create
@article = Article.find(params[:article_id])
if verify_recaptcha
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
else
redirect_to article_path(@article)
end
end
为什么这段代码不起作用?:
def create
@article = Article.find(params[:article_id])
if verify_recaptcha
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
else
render(
html: "<script>alert('Recaptcha error!')</script>".html_safe,
layout: 'application'
)
redirect_to article_path(@article)
end
end
我收到此错误:
AbstractController::DoubleRenderError in CommentsController#create 在此操作中多次调用渲染和/或重定向。
请注意,您只能调用渲染或重定向,并且每个操作最多调用一次。
还要注意redirect和render都不会终止动作的执行,所以如果你想在重定向后退出动作,你需要做类似redirect_to(...) and return的操作。
【问题讨论】:
标签: ruby-on-rails ruby