Skip to content

Instantly share code, notes, and snippets.

@jsmestad
Created September 28, 2011 22:43
Show Gist options
  • Select an option

  • Save jsmestad/1249457 to your computer and use it in GitHub Desktop.

Select an option

Save jsmestad/1249457 to your computer and use it in GitHub Desktop.
$(document).ajaxComplete(function(event, request) {
var flash = $.parseJSON(request.getResponseHeader('X-Flash-Messages'));
if (!flash) return;
if (flash.notice) $('.notice').html(flash.notice);
if (flash.error) alert(flash.error);
}
class ApplicationController < ActionController::Base
protect_from_forgery
after_filter :flash_to_headers
private
def flash_to_headers
return unless request.xhr?
flash_json = Hash[flash.collect do |type, messages|
[k, messages.is_a?(Array) ? messages.collect {|i| ERB::Util.h(i)} : ERB::Util.h(messages)]
end].to_json
response.headers['X-Flash-Messages'] = flash_json
flash.discard
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment