response_with& Flash通知

时间:2011-06-01 16:58:01

标签: ruby-on-rails ruby-on-rails-3

我的一个控制器中有以下内容:

def create

  photo.user = current_user if current_user
  flash[:notice] = "The photos was saved" if photo.save
  respond_with photo, :location => photo_path(photo)

end

此代码只有一个例外。如果我使用某种AJAX访问此操作,然后访问另一个页面,则会显示闪存通知。如何在响应时显示闪存通知:html?我想也许你可以通过:notice => “我的闪光”到respond_with,但没有运气。

2 个答案:

答案 0 :(得分:9)

您可以将条件设​​为:

flash[:notice] = "The photos was saved" if photo.save && !request.xhr?

另外,如果有一天你会在回应AJAX请求时决定使用生成通知, 您可以使用flash.discard清除闪光灯。

答案 1 :(得分:-1)

使用如下:

respond_to do |format|
  format.html { redirect_to photo_path(photo), :notice => 'The photos was saved') }
  format.xml  { render :xml => photo, :status => :created}
end
相关问题