Rails flash messages with AJAX requests
Created
August 21, 2012 01:15
-
-
Save linjunpop/3410235 to your computer and use it in GitHub Desktop.
Rails flash messages with AJAX requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#flash-message | |
- flash.each do |name, msg| | |
= content_tag :div, msg, :id => "flash_#{name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
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 | |
flash.discard # don't want the flash to appear when you reload page | |
end | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
show_ajax_message = (msg, type) -> | |
$("#flash-message").html "<div id='flash-#{type}'>#{msg}</div>" | |
$("#flash-#{type}").delay(5000).slideUp 'slow' | |
$(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 |
Here's my version of the application_controller.rb:
if request.xhr?
#avoiding XSS injections via flash
flash_json = Hash[flash.map{|k,v| [k,ERB::Util.h(v)] }].to_json
response.headers['X-Flash-Messages'] = flash_json
flash.discard
end
do either of these actually work? I haven't had any success
I didn't have any success either. I stripped out the above code and realized the flash was being set through the session. Maybe it will help you.
def ajax_controller_action
if request.xhr?
flash[:notice] = "message."
flash.keep(:notice)
render js: "window.location = #{your_path}"
end
end
-if session["flash"]["flashes"]
- session["flash"]["flashes"].each do |type, message|
= type
= message
Thanks James! This just helped me out.
Yes, Thanks James It even worked out for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for compiling this! It saved me a lot of time. You might be interested in this fork which fixes several bugs, including the one that caused the flash to output
error
if no flash messages were present.