本文介绍了如何在成功提交 AJAX 表单后重定向(Rails 3.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose you have an edit form with :remote => true. Your controller method looks like

 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 版本相同的功能.

What's the best way to do the redirect on successful article update in Rails 3.2.1? The raw JS solution seems a little sleazy, but I do like the fact that it's obvious that it's performing the same function as the format.html version.

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

I would prefer a solution that does not use RJS (if that even works in Rails 3.2?)

推荐答案

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

How about adding the below line of code on the view file itself

#some_file.js.erb

`window.location = redirect_path

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

As you mentioned in the question you do not prefer RJS, but I think it's better to follow this pattern better than writing the js code in the controller.

这篇关于如何在成功提交 AJAX 表单后重定向(Rails 3.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:22