Forum: Ruby on Rails flash[:notice]

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.
4057d36ea1505c263efbdbdc0bf511cd?d=identicon&s=25 D. Krmpotic (rubypassion)
on 2007-01-21 02:24
Hello!

I want to understand more about how flash[:notice] works, because in one
case it stays on longer than I would want or expect..

I set it in on of the actions, and in the view for that action there is
a link to somewhere else.. When the user clicks on that link, the flash
message is displayed in the new action/view.. how come? If I do
flash.now[:notice] = ''.. then it works as expected.. my questions: is
it normal that in first case the message stays.. and why? what is the
exact difference between flash and flash.now... is this subject easy to
understand and consistent or is it a little bit "hackish" and blurry?

The details:

def forgotten_password
    # if post, generate password reset link and store it in the user
table, otherwise just show the form
    if request.post?
      user = User.find_by_email(params[:email])
      if !user
        flash[:notice] = "There is no user with such email"
      else
        Notifier::deliver_password_reset_key(user)
        # show the message about the sent email
        render :action => 'password_reset_sent'
      end
    end
  end


This is the view for this action:

<%= start_form_tag :action => 'forgotten_password' %>
  Plase enter your email:

  <%= text_field_tag(:email, '' ) %>

  <%= submit_tag 'OK' %>
<%= end_form_tag %>

<br/>

<%= link_to "Home", :action => 'login' %>


So when I enter the non-existing email, the above flash message is set
and this view(form) displayed again.. BUT when I click on HOME (link on
the bottom), the message is still there in the 'login' action..

as I said - the message goes away if I use flash.now() .. but I would
really like to understand the philosophy of this.. I don't want to go
testing the code for that kind of stuff and not learning it the right
way..


THANK YOU A LOT for the clarification!

David
70ca58d0e0e0eabbdb74d177417d09d7?d=identicon&s=25 augustlilleaas@gmail.com (Guest)
on 2007-01-21 04:58
(Received via mailing list)
A flash is essensial a session variable that is around in the request
after it was set. On the request after that again, it's gone. Not sure
about your problem, though.

On Jan 21, 2:24 am, David Krmpotic <rails-mailing-l...@andreas-s.net>
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2007-01-21 09:51
(Received via mailing list)
You could use flash.now[:notice], that way it will only show once.

Kind regards,

Nick
--
http://www.railshostinginfo.com
Compare Rails hosting companies
4057d36ea1505c263efbdbdc0bf511cd?d=identicon&s=25 D. Krmpotic (rubypassion)
on 2007-01-21 20:57
thank you, but this doesn't tell me anything ;)

I know that.. and it does work.. but I wanted to know why the normal
flash doesn't.. I also wanted to know the difference between the two
(how it works)..

thank you anyway ... but can anyone provide the *real* explanation?
Thank you!

enjoy!

david


Nick Snels wrote:
> You could use flash.now[:notice], that way it will only show once.
>
> Kind regards,
>
> Nick
> --
> http://www.railshostinginfo.com
> Compare Rails hosting companies
Eac107645eb8b8198558af8af2e3f73c?d=identicon&s=25 Rob Schultz (Guest)
on 2007-01-21 21:06
You have pretty much answered it yourself.
From
http://www.rubyonrails.org/api/classes/ActionContr...

The flash provides a way to pass temporary objects between actions.
Anything you place in the flash will be exposed to the very next action
and then cleared out. This is a great way of doing notices and alerts,
such as a create action that sets flash[:notice] = "Successfully
created" before redirecting to a display action that can then expose the
flash to its template. Actually, that exposure is automatically done.

and for Flash.now
http://www.rubyonrails.org/api/classes/ActionContr...

now()
Sets a flash that will not be available to the next action, only to the
current.
4057d36ea1505c263efbdbdc0bf511cd?d=identicon&s=25 D. Krmpotic (rubypassion)
on 2007-01-22 01:47
OK, I got it now...
The thing that was confusing me was that somehow I thought that regular
flash is cleared after rendering a view (of course it's not), when in
fact it's the next action that is the final stop...

great! Thank you all!

David

Rob Schultz wrote:
> You have pretty much answered it yourself.
> From
> http://www.rubyonrails.org/api/classes/ActionContr...
>
> The flash provides a way to pass temporary objects between actions.
> Anything you place in the flash will be exposed to the very next action
> and then cleared out. This is a great way of doing notices and alerts,
> such as a create action that sets flash[:notice] = "Successfully
> created" before redirecting to a display action that can then expose the
> flash to its template. Actually, that exposure is automatically done.
>
> and for Flash.now
> http://www.rubyonrails.org/api/classes/ActionContr...
>
> now()
> Sets a flash that will not be available to the next action, only to the
> current.
This topic is locked and can not be replied to.