Flash[:notice]

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 %>


<%= 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

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 K. [email protected]

You could use flash.now[:notice], that way it will only show once.

Kind regards,

Nick

http://www.railshostinginfo.com
Compare Rails hosting companies

thank you, but this doesn’t tell me anything :wink:

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 S. 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

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 S. wrote:

You have pretty much answered it yourself.
From
http://www.rubyonrails.org/api/classes/ActionController/Flash.html

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/ActionController/Flash/FlashHash.html

now()
Sets a flash that will not be available to the next action, only to the
current.

You have pretty much answered it yourself.
From
http://www.rubyonrails.org/api/classes/ActionController/Flash.html

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/ActionController/Flash/FlashHash.html

now()
Sets a flash that will not be available to the next action, only to the
current.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs