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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With