# DRY way of updating flash in AJAX

**URL:** <https://www.ruby-forum.com/t/dry-way-of-updating-flash-in-ajax/171966>\
**Category:** Rails\
**Created:** [July 12, 2009, 2:16pm UTC](https://www.ruby-forum.com/t/dry-way-of-updating-flash-in-ajax/171966 "2009-07-12T14:16:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [July 12, 2009, 2:16pm UTC](https://www.ruby-forum.com/t/dry-way-of-updating-flash-in-ajax/171966/1 "2009-07-12T14:16:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [July 12, 2009, 2:36pm UTC](https://www.ruby-forum.com/t/dry-way-of-updating-flash-in-ajax/171966/2 "2009-07-12T14:36:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [July 13, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/dry-way-of-updating-flash-in-ajax/171966/3 "2009-07-13T20:06:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [July 13, 2009, 10:07pm UTC](https://www.ruby-forum.com/t/dry-way-of-updating-flash-in-ajax/171966/4 "2009-07-13T22:07:53Z")

</div>

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?
