From the course: Ruby on Rails 6: Controllers and Views

Messaging with the flash hash - Ruby on Rails Tutorial

From the course: Ruby on Rails 6: Controllers and Views

Start my 1-month free trial

Messaging with the flash hash

- [Instructor] Another useful tool in Rails controllers, is something called the FlasHash, that's used for simple messaging. The way the FlashHash works, is that it stores a message in the session data. Then when the user makes another request, the site will recognize them based on their session cookie, and the message will be available for them to view. And you could do that on your own, right? You could put information into the session, and then retrieve it back out again, we learned that in the last movie. What's special about the FlashHash, is that it automatically clears those old messages after every single request. That means that when you put a message into the FlashHash, it goes in the session, and it sticks around for one additional request response cycle, and then it's gone, it's disposed off. Usually, that means that it's there through one redirect. That's the time that it's most used. There's a common need in a web application. The user makes a request, and the web application performs some action. It wants to have the results of that action, to persist, even though it's going to redirect the user to a new page. The most common time you're going to need it, for example let's just say, that it successfully performed the action, or that there was an error in the process. FlashHash is just as easy as using cookies and sessions. It works just like a hash. So we have flash, and then in square brackets, the key that we want to use. Here you can see I'm using notice and error. These are the two most common used in Rails, notice by far is the most common one, but you can use any key you want. The Flash is going to get rid of every value after every request. So it's up to you what you use. The most common reason people use different ones, is so they can apply different CSS styling to each one, and maybe the flash notice will be green, or flash error will be red. And then we can retrieve those values back out, just like if we were working with cookies or sessions. Let's try it. So in our application, we already seen that we have this fake login page. And it's redirecting the user to the menu path. So let's add a reference here, where we say, flash, say notice, equals, and we'll say, Log in successful. Now we're going to set that value, if this were just a message, let's say we just had it set to something like @message, well, the moment we redirect it'll be gone, it would not persist. But what we're doing here is we're actually storing it in the session, so that it will persist, even though the browser is going to make a new web request, to get the menu. So we'll be available here. And we don't need to pull up that value here, instead, we're going to pull it up on the page itself. And I'm actually just going to copy and paste in a snippet of HTML here at the top. You can pause the movie if you want to copy it down, but it's very simple. It just says if the flash notice is present, then put a div which I've called ID flash notice and I've given it some styling, just basically put a red border around it, made the text red, a little bit of padding, and then display whatever's in the flash notice. That's it, very simple. So let's save it, and let's try it out. Let's come back over here, and let's hit the back button till we get to our login form. My server is not running, let me do that rails s. Once it says it's listening, let's reload that page. And I'm going to make the login now, Mary, and I'll hit login. And you see it says Login successful. There it is, that's the flash message that it put there. It persisted, even though it though it redirected. I'm now on a different URL, I'm on menu, I'm not on new, I'm not on create, I'm on menu. And then if we reload that page, reload the menu page, the menu reloads, but notice the flash notice is gone, because it automatically cleared it out for me. So that's when you use that flash notice is right before redirect, when you want to store something, a simple message, don't store complex objects or anything like that in there, that's not what it's for. Just store very simple messages, that will persist after the redirect.

Contents