如何在成功提交AJAX表单后重定向(Rails 3.2)

假设您有一个编辑表单:remote => true 。 你的控制器方法看起来像

  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版本相同的function。

我更喜欢不使用RJS的解决方案( 如果它甚至适用于Rails 3.2 ?)

如何在视图文件本身上添加以下代码行

 #some_file.js.erb `window.location = redirect_path 

正如你在问题中所提到的,你不喜欢RJS,但我认为最好比在控制器中编写js代码更好地遵循这种模式。

你的ajax是否与模型(.php,.asp?)相互作用。 在这种情况下,我首选的方法是在提交后在模型中创建成功/失败标准,并直接从那里重定向。 不确定在这个应用程序中是否有意义吗?