Forum: Ruby on Rails flash not clearing after display

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
47d0bfbb5b81fa04559b0b7790d17106?d=identicon&s=25 Bill (Guest)
on 2006-06-01 22:34
(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
F10f95926f620318a71c102f207649b7?d=identicon&s=25 Manish Shah (Guest)
on 2006-06-02 19:38
(Received via mailing list)
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?
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-06-02 19:59
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.
F10f95926f620318a71c102f207649b7?d=identicon&s=25 Manish Shah (Guest)
on 2006-06-02 20:09
(Received via mailing list)
I am using flash with redirects.  However its not being cleared.  I cant
tell whats going on.
5233478c51a92b6a1a5c970cbf3a42f3?d=identicon&s=25 Isak Hansen (Guest)
on 2006-06-02 20:45
(Received via mailing list)
On 6/2/06, Manish Shah <mnshah@gmail.com> wrote:
> I am using flash with redirects.  However its not being cleared.  I cant
> tell whats going on.
>
> On 6/2/06, Alex Wayne < rubyonrails@beautifulpixel.com> 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
D521c0db52fe0b14d799dd669d536098?d=identicon&s=25 Tony Mobily (merc)
on 2006-06-23 06:00
Hi,

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

Merc.

Isak Hansen wrote:
> On 6/2/06, Manish Shah <mnshah@gmail.com> wrote:
>> I am using flash with redirects.  However its not being cleared.  I cant
>> tell whats going on.
>>
>> On 6/2/06, Alex Wayne < rubyonrails@beautifulpixel.com> 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
This topic is locked and can not be replied to.