Ruby on rails 3 如何在rails3中呈现编辑视图和发布flash消息

Ruby on rails 3 如何在rails3中呈现编辑视图和发布flash消息,ruby-on-rails-3,view,Ruby On Rails 3,View,在我的帐户控制器中,我希望在保存更改后显示(渲染、重定向到?)编辑视图,并显示flash通知 def update @account = Account.find(params[:id]) respond_to do |format| if @account.update_attributes(params[:account]) format.html { redirect_to(@account, :notice => 'Account w

在我的帐户控制器中,我希望在保存更改后显示(渲染、重定向到?)编辑视图,并显示flash通知

 def update
    @account = Account.find(params[:id])

    respond_to do |format|
      if @account.update_attributes(params[:account])
        format.html { redirect_to(@account, :notice => 'Account was successfully updated.') }

      else
        format.html { render :action => "edit" }
      end
    end
  end

您仍然可以使用Rails 2中的通知:

flash[:notice] = "message"
只需在视图顶部添加以下行即可显示:


如果您不想让用户再次填写编辑表单,您应该使用
render
方法。

默认情况下,您必须使用单独的语句,例如

format.html { 
  flash[:notice] = 'message'
  render :edit
}

有一个补丁允许您使用
渲染“编辑”,注意=>“消息”
。它没有进入Rails,但有一个gem添加了它。

如果您只使用
flash[:注意]
该值在下一个请求中仍然可用。意思是,你将在接下来的两页中看到文本。改为使用
flash.now
仅使该值在当前请求中可用

format.html { 
  flash.now[:notice] = 'message'
  render :edit
}

参考阅读

我不明白你最后的评论。标准做法是在更新后将您重定向到,这样浏览器刷新就不会再次提交。“再次填写编辑表单”部分让我感到困惑。实际上这是我的错误,在你的情况下,这些方法之间真的没有区别。所以你可以随意使用。@timkay但是别忘了
render
redirect\u to
是完全不同的方法。阅读了解更多信息。@timkay在更新失败时重定向将清除表单中以前编辑过的数据,这对最终用户来说非常烦人。如果您使用的是
render
(与
redirect\u to
)相反,您通常希望使用
flash。现在
。我详细解释了原因。