Gabrijel Šimunović
Gabrijel Šimunović

Reputation: 95

How do flash messages work in Rails?

How does the framework associate flash messages with users? How does it know when to apply it, i.e. after redirect?

Imagine a situation when in between adding a flash and loading the page the user was redirected to there comes some totally unconnected Ajax request. Will the Rails apply flash notices to the Ajax request?

Upvotes: 2

Views: 1259

Answers (2)

tee_zed_0
tee_zed_0

Reputation: 43

For rails 7

https://guides.rubyonrails.org/action_controller_overview.html#the-flash

The flash is a special part of the session which is cleared with each request.

Moreover, how session itself stored is detemined by session store

  • ActionDispatch::Session::CookieStore - Stores everything on the client.
  • ActionDispatch::Session::CacheStore - Stores the data in the Rails cache.
  • ActionDispatch::Session::MemCacheStore - Stores the data in a memcached cluster (this is a legacy implementation; consider using CacheStore instead)
  • ActionDispatch::Session::ActiveRecordStore - Stores the data in a database using Active Record (requires the activerecord-session_store gem) A custom store or a store provided by a third party gem

Upvotes: 1

Neil
Neil

Reputation: 5188

I don't think flash hashes are associated with users. They are stored in the sessions cookie on the user's browser in order to persist for the next request. For the request following that one the flash hash clears itself out.

Upvotes: 3

Related Questions