Forum: Ruby on Rails flash not clearing?

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.
Jeff E. (Guest)
on 2007-05-01 17:53
(Received via mailing list)
hi all,

using rails 1.2.3 on windows xp

i have a controller method like so:

def index
  ...do stuff...
  if some_error
    flash[:notice] = "error!"
  end
end

and the layout has:
  <% if flash[:notice] -%>
  <div class="flash"><%= flash[:notice] %></div>
  <% end -%>

the form for index submits via GET method.  if i cause the some_error
condition through the form, i get the flash message as expected.
however, if i immediately submit again with conditions that do not
cause some_error, i still get the flash message again.  it takes until
submitting a third time for the flash message to clear.  isn't it
supposed to clear itself after it renders the first time in the
layout?  i know i'm not setting it again because i have a debug
statement when it gets set in the controller.
Jeff E. (Guest)
on 2007-05-01 17:58
(Received via mailing list)
ok, nevermind - i RTFM:
http://api.rubyonrails.org/classes/ActionControlle...

the method
flash.now[:message] = "Hello current action"

is supposed to be used when rendering the current action;
flash[:notice] = 'some message' is to be used when using redirect_to
Hal (Guest)
on 2007-05-01 17:59
(Received via mailing list)
What you describes maps with my understanding of flash.  From
ActiveController:Flash: "Anything you place in the flash will be
exposed to the very next action and then cleared out."

I think what you want is flash.now:
 flash.now[:notice] = "error!"

 - Hal
Jeff E. (Guest)
on 2007-05-01 18:27
(Received via mailing list)
yep - just saw that in the docs too.  thanks for the reply!
This topic is locked and can not be replied to.