Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

respond_with & Flash Notice

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

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.

like image 904
Kyle Decot Avatar asked Jun 01 '11 16:06

Kyle Decot


1 Answers

You can put condition like:

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

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

like image 174
taro Avatar answered Oct 20 '22 10:10

taro