views:

101

answers:

1

Hi folks,

I'm looking for some advice on the best way to provide a single place where ajax status/error messages are displayed for a rails app. This is similar to the rails flash subsystem, but for ajax requests.

Google Reader and gmail are good examples that have this functionality.

Any chance there is a simple plugin that provides this?

Thanks.

A: 

In your layout:

<div id="flash_container"></div>

In your AJAX response

<script type='text/javacript'>
document.getElementById('flash_container').innerHTML="<%=escape_javascript render_flash %>";
</script>

In your helpers

def render_flash

  html = ""

  flash.each do |key,value|
    html << "<div class="flash_#{key}">#{h value}</div>"
  end

  html
end

Maybe, of course, it could be a plugin :)

Leonid Shevtsov
Sounds like a good solution, though would be better off to use something like jQuery to do this and not write the flash_container div unless you REALLY need it (e.g. when you have to display a flash), saves you writing unnecessary HTML in the layout.
Omar Qureshi
I'd keep the empty div. Otherwise, your JavaScript would need to know where the flash goes on the page, which gets even messier as soon as you try to add a 2nd layout.
jdl
But with innerHTML the DIV stays on the page, only its contents get changed. I actually do this with jQuery, just wanted to provide a library-agnostic solution. :)
Leonid Shevtsov