Flash not clearing after display


#1

(Rails 1.1, Ruby 1.8.4)

In the layout for one of my controllers, I have a tag to display a flash
message (copied from AWDWR), like this:

 <div id="data">
    <% if @flash[:notice] -%>
      <div id="notice"><%= @flash[:notice] %></div>
    <% end -%>
    <%= @content_for_layout %>
  </div>

If the controller sets the message, it is displayed as expected: so far
so good.

But if I then navigate to a different action of the same controller, the
flash isn’t cleared, where I thought it was supposed to be cleared out
automatically when the current rendering was complete. On the second
subsequent call to the same controller it does disappear.

If I clear it manually after displaying it, as follows, then all is
well.

<% @flash[:notice] = nil; end -%>

So I have a work-around, but can anyone help me understand what is going
on: am I doing something wrong, or misunderstanding how the flash is
meant to work?

Thanks in advance

Bill


#2

i am having the same problem. Are you using the db to store your
sessions?
I started noticing this problem when i switched from file storage to
database storage. I dont know if it makes a difference or why it should
matter at all.

Can anyone shed some more light here?


#3

Bill wrote:

(Rails 1.1, Ruby 1.8.4)
So I have a work-around, but can anyone help me understand what is going
on: am I doing something wrong, or misunderstanding how the flash is
meant to work?

Thanks in advance

Bill

The flash is meant to display something in next action, not the current
one. It is cleared in the request after it is created

For example:

def create
@foo = Foo.new(params[:foo])
if @foo.save
flash[:notice] = ‘Foo was created successfully’
redirect_to :action => ‘show’, :id => @foo.id
else
render :action => ‘new’
end
end

Now when the user submits valid data for a “foo” the flash says confirms
everthing went smooth, and the browser is redirected to make a new
request, where the flash is displayed.

If you do a flash without a redirect, you will see the flash on the
current page you just rendered AND the next page, and then it should be
cleared.

For this reason flash should primarily be used with redirect.


#4

I am using flash with redirects. However its not being cleared. I cant
tell whats going on.


#5

On 6/2/06, Manish S. removed_email_address@domain.invalid wrote:

I am using flash with redirects. However its not being cleared. I cant
tell whats going on.

On 6/2/06, Alex W. < removed_email_address@domain.invalid> wrote:

The flash is meant to display something in next action, not the current
one. It is cleared in the request after it is created

I’ve found that redirects from a before_filter don’t affect the flash,
i.e. it stays for the next two requests.
Perhaps you have the same issue, and is this working as intended?

I fix this using flash.now instead of just flash in the filter.

HTH,
Isak


#6

Hi,

This was exactly my problem. It was driving me insane.
Thank you so much for pointing it out!

Merc.

Isak H. wrote:

On 6/2/06, Manish S. removed_email_address@domain.invalid wrote:

I am using flash with redirects. However its not being cleared. I cant
tell whats going on.

On 6/2/06, Alex W. < removed_email_address@domain.invalid> wrote:

The flash is meant to display something in next action, not the current
one. It is cleared in the request after it is created

I’ve found that redirects from a before_filter don’t affect the flash,
i.e. it stays for the next two requests.
Perhaps you have the same issue, and is this working as intended?

I fix this using flash.now instead of just flash in the filter.

HTH,
Isak