2012-02-29 125 views
3

假设您有一个编辑表单,其编号为:remote => true。您的控制器方法看起来像如何在成功提交AJAX表单后重定向(Rails 3.2)

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?)

回答

0

贵阿贾克斯与模型进行交互的解决方案(.PHP,.ASP?)。在这种情况下,我首选的方法是在提交后在模型内创建成功/失败标准,并直接从那里重定向。不知道在这个应用程序中是否有意义?

1

如何添加下面的一行代码在视图文件本身

#some_file.js.erb 

`window.location = redirect_path 

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