DRY way of updating flash in AJAX

At this moment I'm implementing errors for my application. I have created a div for notices and warnings. The application only does AJAX requests, so it updates both flashes every time a request is made by the user.

At this moment, I add a page.replace to every .rjs file at the end to update the flashes. If flash[:warning] is nil, it will remove any previous warnings and a flash[:notice] is added. It works, though this is not very DRY. What I would like to see is that with every AJAX- request, these flashes are updated automatically. I guess what I want is what any .rhtml file does in /app/views/layouts/, but now for .rjs files. How can this be done?

I hope you understand my question. If you have a better way to report errors with AJAX requests, please let me know. I'm eager to learn!

In addition to this, if possible, I would like the application to know whether to execute the lines in the .rjs file or display a warning by evaluating flash[:warning].

So, if I have a form in which you can add an object, I would automatically want to do:

  page.insert_html :bottom, :object_list, :partial => @object #only this line would actually be in the .rjs file   page.replace_html :notice, flash[:notice]   flash.discard

every time it actually does something and:

  page.replace_html :notice, flash[:warning]   flash.discard

every time an error occurs.

I still haven't found how to do this. The only thing I could think about was adding a .rjs file for the controller to /app/layouts, but that doesn't work.

Anyone?

I have a thought ...

I could add a general respond_to to the end of my controller which responds to every AJAX-request. However, I'm unsure how to do this. I've tried a few things, but none of them seem to work the way I want them to.

If this would work I would be able to evaluate flash[:warning] and based on that decide whether to go on without having to do anything else. Both flashes will be updated anyway, so on a request that doesn't have a response, both flashes will be empty.

Back to my question: how can I have my controller respond_to every AJAX-request?