response_with &快讯 [英] respond_with & Flash Notice

查看:18
本文介绍了response_with &快讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following in one of my controllers:

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 访问此操作,然后访问另一个页面,则会显示 Flash 通知.如何在使用 :html 响应时仅显示 Flash 通知?我想也许你可以将 :notice => "my flash" 传递给 respond_with 但没有运气.

This code works with one exception. If I access this action w/ some kind of AJAX, and then visit another page, the flash notice will get displayed. How can I only display the flash notice when responding with :html? I thought maybe you could pass a :notice => "my flash" to respond_with but no luck.

推荐答案

你可以输入如下条件:

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

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

Also, if some day you will decide use generated notice when responding to AJAX request, you can use flash.discard to clear flash.

这篇关于response_with &快讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆