Topic: Clear flash message

How can I clear a flash notice without reloading the page? I guess I'm after something like page.replace_html and set the innerHTML to nothing. But I only know how to use rjs with ajax and do:

render :update do |page|


end


Can I run rjs without the above render :update?

Re: Clear flash message

What exactly specifically are you trying to do? Do you only want to display the flash messages for 5 seconds or something? Are you already using AJAX anywhere?

Railscasts - Free Ruby on Rails Screencasts

Re: Clear flash message

ryanb wrote:

What exactly specifically are you trying to do? Do you only want to display the flash messages for 5 seconds or something? Are you already using AJAX anywhere?

Yes. The problem I have is that I partially use Ajax and partially the "old" way. More specific I couldn't get the validation in the model to work for an array or checkboxes, so I run a validation in the controller. After hitting the submit for this particular form you get to the create action and I use a regular redirect_to which sends you back to the page if an validation fails with a flash[:notice] error message. The actual form comes forward when you hit a link_to_remote on that page using Ajax, so it's not ther by default. The problem is that the error message is still there, because the Ajax will load the from in a otherwise hidden div. So either clear the flash or get a validation in the model to work with checkboxes

Re: Clear flash message

You can do this in your RJS:

page[:notice].replace_html flash[:notice]

That is, assuming there's an HTML element with the id of "notice". This will set the contents of that element to whatever is currently in flash[:notice] which will likely be an empty string/nil.

If you want to learn more about RJS I recommend this Railscasts episode.

Railscasts - Free Ruby on Rails Screencasts

Re: Clear flash message

ryanb wrote:

You can do this in your RJS:

page[:notice].replace_html flash[:notice]

That is, assuming there's an HTML element with the id of "notice". This will set the contents of that element to whatever is currently in flash[:notice] which will likely be an empty string/nil.

If you want to learn more about RJS I recommend this Railscasts episode.

I second that recommendation.  Railscast is my favorite podcast right now. The RJS episodes cleared up every confusion I had with RJS.