On index
page of MyController
I set a value to flash[:notice]
:
class MyController < ApplicationController
def index
flash[:notice] = "my message"
...
end
end
I do see "my message"
displayed as expected.
However, when I click a link on this page that points to index
page of MyOtherController
, I still see "my message"
:
class MyOtherController < ApplicationController
def index
puts "----------------------------------------"
puts flash[:notice] # => "my message"
puts "----------------------------------------"
end
end
I thought that flash[:notice]
becomes empty with every request, but here this is not the case. What is the correct way to empty flash[:notice]
?
you can use flash.now[:notice] = ...
instead. flash.now
is useful when you dont want the flash message to persist to the next request. Often a redirect_to
follows a flash[:notice] = ...
which is why it is persisted for one request
Most of the time this rule should be correct:
Use flash[:notice]
with redirect
Use flash.now[:notice]
with render
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With