Skip to content

Instantly share code, notes, and snippets.

@birchestx
Forked from hbrandl/RailsFlashToHeaders.md
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save birchestx/1c2357ca46a9c2a08934 to your computer and use it in GitHub Desktop.

Select an option

Save birchestx/1c2357ca46a9c2a08934 to your computer and use it in GitHub Desktop.

Revisions

  1. Karen Baker revised this gist Jan 25, 2015. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,10 @@ $(document).ajaxComplete (event, request) ->
    </div>
    </div>") if msg
    #delete the flash message (if it was there before) when an ajax request returns no flash message
    $("#flash_hook").replaceWith("<div id='flash_hook'></div>") unless msg
    $("#flash_hook").replaceWith("<div id='flash_hook'></div>") unless msg

    jQuery ->
    # empty the flash hook when close modal
    $(".modal").on "hidden.bs.modal", ->
    $("#flash_hook").empty()
    return
  2. @hbrandl hbrandl revised this gist Jan 18, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # encoding: UTF-8
    class ApplicationController < ActionController::Base
    protect_from_forgery

  3. @hbrandl hbrandl revised this gist Jan 18, 2014. 2 changed files with 4 additions and 1 deletion.
    File renamed without changes.
    5 changes: 4 additions & 1 deletion application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,10 @@ class ApplicationController < ActionController::Base

    def flash_to_headers
    return unless request.xhr?
    response.headers['X-Message'] = flash_message
    msg = flash_message
    #replace german umlaute encoded in utf-8 to html escaped ones
    msg = msg.gsub("ä","&auml;").gsub("ü","&uuml;").gsub("ö","&ouml;").gsub("Ä","&Auml;").gsub("Ü","&Uuml;").gsub("Ö","&Ouml;").gsub("ß","&szlig;")
    response.headers['X-Message'] = msg
    response.headers["X-Message-Type"] = flash_type.to_s

    flash.discard # don't want the flash to appear when you reload page
  4. @hbrandl hbrandl revised this gist Jan 18, 2014. 3 changed files with 22 additions and 14 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Rails flash messages with AJAX requests & twitter-bootstrap
    Rails flash messages with AJAX requests & twitter-bootstrap 2.3

    Forked from https://gist.github.com/linjunpop/3410235
    Based on Stackoverflow answer: http://stackoverflow.com/a/10167659/656428
    7 changes: 6 additions & 1 deletion application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,13 @@ def flash_message
    end

    def flash_type
    [:error, :warning, :notice].each do |type|
    #:keep will instruct the js to not update or remove the shown message.
    #just write flash[:keep] = true (or any other value) in your controller code
    [:error, :warning, :notice, :keep].each do |type|
    return type unless flash[type].blank?
    end
    #don't return the array from above which would happen if we don't have an explicit return statement
    #returning :empty will also allow you to easily know that no flash message was transmitted
    return :empty
    end
    end
    27 changes: 15 additions & 12 deletions flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,25 @@
    #ajax call to show flash messages when they are transmitted in the header
    #this code assumes the following
    # 1) you're using twitter-bootstrap (although it will work if you don't)
    # 1) you're using twitter-bootstrap 2.3 (although it will work if you don't)
    # 2) you've got a div with the id flash_hook somewhere in your html code

    $(document).ajaxComplete (event, request) ->
    msg = request.getResponseHeader("X-Message")
    alert_type = 'alert-success'
    alert_type = 'alert-error' unless request.getResponseHeader("X-Message-Type").indexOf("error") is -1
    $("#flash_hook").replaceWith("<div id='flash_hook'>
    <p>&nbsp;</p>
    <div class='row'>
    <div class='span10 offset1'>
    <div class='alert " + alert_type + "'>
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    " + msg + "

    unless request.getResponseHeader("X-Message-Type").indexOf("keep") is 0
    #add flash message if there is any text to display
    $("#flash_hook").replaceWith("<div id='flash_hook'>
    <p>&nbsp;</p>
    <div class='row'>
    <div class='span10 offset1'>
    <div class='alert " + alert_type + "'>
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    " + msg + "
    </div>
    </div>
    </div>
    </div>
    </div>") if msg
    #delete the flash message (if it was there before) when an ajax request returns no flash message
    $("#flash_hook").replaceWith("<div id='flash_hook'></div>") unless msg
    </div>") if msg
    #delete the flash message (if it was there before) when an ajax request returns no flash message
    $("#flash_hook").replaceWith("<div id='flash_hook'></div>") unless msg
  5. @hbrandl hbrandl revised this gist Dec 26, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,10 @@ class ApplicationController < ActionController::Base

    def flash_to_headers
    return unless request.xhr?
    response.headers['X-Message'] = flash_message
    msg = flash_message
    #replace german umlaute encoded in utf-8 to html escaped ones
    msg = msg.gsub("ä","&auml;").gsub("ü","&uuml;").gsub("ö","&ouml;").gsub("Ä","&Auml;").gsub("Ü","&Uuml;").gsub("Ö","&Ouml;").gsub("ß","&szlig;")
    response.headers['X-Message'] = msg
    response.headers["X-Message-Type"] = flash_type.to_s

    flash.discard # don't want the flash to appear when you reload page
  6. @hbrandl hbrandl renamed this gist Jul 26, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    Rails flash messages with AJAX requests & twitter-bootstrap

    Forked from https://gist.github.com/linjunpop/3410235
    Based on Stackoverflow answer: http://stackoverflow.com/a/10167659/656428
    Based on Stackoverflow answer: http://stackoverflow.com/a/10167659/656428

    License: MIT
  8. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,6 @@ $(document).ajaxComplete (event, request) ->
    </div>
    </div>
    </div>
    </div>") if msg
    </div>") if msg
    #delete the flash message (if it was there before) when an ajax request returns no flash message
    $("#flash_hook").replaceWith("<div id='flash_hook'></div>") unless msg
  9. @hbrandl hbrandl revised this gist Mar 27, 2013. No changes.
  10. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -7,12 +7,14 @@ $(document).ajaxComplete (event, request) ->
    msg = request.getResponseHeader("X-Message")
    alert_type = 'alert-success'
    alert_type = 'alert-error' unless request.getResponseHeader("X-Message-Type").indexOf("error") is -1
    $("#flash_hook").replaceWith("<p>&nbsp;</p>
    $("#flash_hook").replaceWith("<div id='flash_hook'>
    <p>&nbsp;</p>
    <div class='row'>
    <div class='span10 offset1'>
    <div class='alert " + alert_type + "'>
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    " + msg + "
    </div>
    </div>
    </div>") if msg
    </div>
    </div>") if msg
  11. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -7,12 +7,14 @@ $(document).ajaxComplete (event, request) ->
    msg = request.getResponseHeader("X-Message")
    alert_type = 'alert-success'
    alert_type = 'alert-error' unless request.getResponseHeader("X-Message-Type").indexOf("error") is -1
    $("#flash_hook").replaceWith("<p>&nbsp;</p>
    $("#flash_hook").replaceWith("<div id='flash_hook'>
    <p>&nbsp;</p>
    <div class='row'>
    <div class='span10 offset1'>
    <div class='alert " + alert_type + "'>
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    " + msg + "
    </div>
    </div>
    </div>") if msg
    </div>
    </div>") if msg
  12. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    Rails flash messages with AJAX requests
    Rails flash messages with AJAX requests & twitter-bootstrap

    original: http://stackoverflow.com/a/10167659/656428
    Forked from https://gist.github.com/linjunpop/3410235
    Based on Stackoverflow answer: http://stackoverflow.com/a/10167659/656428
  13. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,18 @@
    show_ajax_message = (msg, type) ->
    $("#flash-message").html "<div id='flash-#{type}'>#{msg}</div>"
    $("#flash-#{type}").delay(5000).slideUp 'slow'
    #ajax call to show flash messages when they are transmitted in the header
    #this code assumes the following
    # 1) you're using twitter-bootstrap (although it will work if you don't)
    # 2) you've got a div with the id flash_hook somewhere in your html code

    $(document).ajaxComplete (event, request) ->
    msg = request.getResponseHeader("X-Message")
    type = request.getResponseHeader("X-Message-Type")
    show_ajax_message msg, type #use whatever popup, notification or whatever plugin you want
    alert_type = 'alert-success'
    alert_type = 'alert-error' unless request.getResponseHeader("X-Message-Type").indexOf("error") is -1
    $("#flash_hook").replaceWith("<p>&nbsp;</p>
    <div class='row'>
    <div class='span10 offset1'>
    <div class='alert " + alert_type + "'>
    <button type='button' class='close' data-dismiss='alert'>&times;</button>
    " + msg + "
    </div>
    </div>
    </div>") if msg
  14. @hbrandl hbrandl revised this gist Mar 27, 2013. 2 changed files with 9 additions and 3 deletions.
    9 changes: 9 additions & 0 deletions application.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <!-- flash_message.js.coffee expects to find a div with this id in your html code -->
    <div id="flash_hook"></div>
    </body>
    </html>
    3 changes: 0 additions & 3 deletions application.html.slim
    Original file line number Diff line number Diff line change
    @@ -1,3 +0,0 @@
    #flash-message
    - flash.each do |name, msg|
    = content_tag :div, msg, :id => "flash_#{name}"
  15. @hbrandl hbrandl revised this gist Mar 27, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@ def flash_message
    [:error, :warning, :notice].each do |type|
    return flash[type] unless flash[type].blank?
    end
    # if we don't return something here, the above code will return "error, warning, notice"
    return ''
    end

    def flash_type
  16. @linjunpop linjunpop revised this gist Aug 28, 2012. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,27 @@
    class ApplicationController < ActionController::Base
    protect_from_forgery
    protect_from_forgery

    after_filter :flash_to_headers
    after_filter :flash_to_headers

    private

    def flash_to_headers
    return unless request.xhr?
    response.headers['X-Message'] = flash_message
    response.headers["X-Message-Type"] = flash_type.to_s
    return unless request.xhr?
    response.headers['X-Message'] = flash_message
    response.headers["X-Message-Type"] = flash_type.to_s

    flash.discard # don't want the flash to appear when you reload page
    flash.discard # don't want the flash to appear when you reload page
    end

    private

    def flash_message
    [:error, :warning, :notice].each do |type|
    return flash[type] unless flash[type].blank?
    end
    [:error, :warning, :notice].each do |type|
    return flash[type] unless flash[type].blank?
    end
    end

    def flash_type
    [:error, :warning, :notice].each do |type|
    return type unless flash[type].blank?
    end
    [:error, :warning, :notice].each do |type|
    return type unless flash[type].blank?
    end
    end
    end
  17. @linjunpop linjunpop revised this gist Aug 21, 2012. 1 changed file with 3 additions and 10 deletions.
    13 changes: 3 additions & 10 deletions flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,8 @@
    # FLASH NOTICE ANIMATION
    fade_flash = ->
    $("#flash_notice").delay(5000).fadeOut "slow"
    $("#flash_alert").delay(5000).fadeOut "slow"
    $("#flash_error").delay(5000).fadeOut "slow"

    fade_flash()
    show_ajax_message = (msg, type) ->
    $("#flash-message").html "<div id='flash_#{type}'>#{msg}</div>"
    fade_flash()
    $("#flash-message").html "<div id='flash-#{type}'>#{msg}</div>"
    $("#flash-#{type}").delay(5000).slideUp 'slow'

    $("#flash-message").ajaxComplete (event, request) ->
    $(document).ajaxComplete (event, request) ->
    msg = request.getResponseHeader("X-Message")
    type = request.getResponseHeader("X-Message-Type")
    show_ajax_message msg, type #use whatever popup, notification or whatever plugin you want
  18. @linjunpop linjunpop revised this gist Aug 21, 2012. 2 changed files with 15 additions and 18 deletions.
    18 changes: 0 additions & 18 deletions application.js
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    // FLASH NOTICE ANIMATION
    var fade_flash = function() {
    $("#flash_notice").delay(5000).fadeOut("slow");
    $("#flash_alert").delay(5000).fadeOut("slow");
    $("#flash_error").delay(5000).fadeOut("slow");
    };
    fade_flash();

    var show_ajax_message = function(msg, type) {
    $("#flash-message").html('<div id="flash_'+type+'">'+msg+'</div>');
    fade_flash();
    };

    $("#flash-message").ajaxComplete(function(event, request) {
    var msg = request.getResponseHeader('X-Message');
    var type = request.getResponseHeader('X-Message-Type');
    show_ajax_message(msg, type); //use whatever popup, notification or whatever plugin you want
    });
    15 changes: 15 additions & 0 deletions flash_message.js.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # FLASH NOTICE ANIMATION
    fade_flash = ->
    $("#flash_notice").delay(5000).fadeOut "slow"
    $("#flash_alert").delay(5000).fadeOut "slow"
    $("#flash_error").delay(5000).fadeOut "slow"

    fade_flash()
    show_ajax_message = (msg, type) ->
    $("#flash-message").html "<div id='flash_#{type}'>#{msg}</div>"
    fade_flash()

    $("#flash-message").ajaxComplete (event, request) ->
    msg = request.getResponseHeader("X-Message")
    type = request.getResponseHeader("X-Message-Type")
    show_ajax_message msg, type #use whatever popup, notification or whatever plugin you want
  19. @linjunpop linjunpop created this gist Aug 21, 2012.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Rails flash messages with AJAX requests

    original: http://stackoverflow.com/a/10167659/656428
    3 changes: 3 additions & 0 deletions application.html.slim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    #flash-message
    - flash.each do |name, msg|
    = content_tag :div, msg, :id => "flash_#{name}"
    18 changes: 18 additions & 0 deletions application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // FLASH NOTICE ANIMATION
    var fade_flash = function() {
    $("#flash_notice").delay(5000).fadeOut("slow");
    $("#flash_alert").delay(5000).fadeOut("slow");
    $("#flash_error").delay(5000).fadeOut("slow");
    };
    fade_flash();

    var show_ajax_message = function(msg, type) {
    $("#flash-message").html('<div id="flash_'+type+'">'+msg+'</div>');
    fade_flash();
    };

    $("#flash-message").ajaxComplete(function(event, request) {
    var msg = request.getResponseHeader('X-Message');
    var type = request.getResponseHeader('X-Message-Type');
    show_ajax_message(msg, type); //use whatever popup, notification or whatever plugin you want
    });
    27 changes: 27 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    class ApplicationController < ActionController::Base
    protect_from_forgery

    after_filter :flash_to_headers

    def flash_to_headers
    return unless request.xhr?
    response.headers['X-Message'] = flash_message
    response.headers["X-Message-Type"] = flash_type.to_s

    flash.discard # don't want the flash to appear when you reload page
    end

    private

    def flash_message
    [:error, :warning, :notice].each do |type|
    return flash[type] unless flash[type].blank?
    end
    end

    def flash_type
    [:error, :warning, :notice].each do |type|
    return type unless flash[type].blank?
    end
    end
    end