假设您有一个带有 :remote => true
的编辑表单.您的 Controller 方法看起来像
def update
@article = Article.find(params[:id])
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to @article}
format.js { render :js => "window.location.replace('#{article_path(@article)}');"}
else
format.html { render :action => "edit" }
# Render update.js.erb which replaces the body of the form
format.js {}
end
end
end
在 Rails 3.2.1 中成功更新文章时重定向的最佳方法是什么?原始 JS 解决方案似乎有点低俗,但我确实喜欢这样一个事实,即它显然执行与
format.html
相同的功能。版本。我更喜欢不使用 RJS 的解决方案(if that even works in Rails 3.2?)
最佳答案
如何在 View 文件本身上添加以下代码行
#some_file.js.erb
`window.location = redirect_path
正如您在问题中提到的,您不喜欢 RJS,但我认为遵循这种模式比在 Controller 中编写 js 代码更好。
关于ruby-on-rails - 成功提交 AJAX 表单后如何重定向 (Rails 3.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506445/