So let's finish up this passage to make a reservation for form, and what we want to do is if someone
tries to go to a reservation summary, I have to type the URL because there's no direct link to it.
We want them to be redirected somewhere else on the site and to be given an error message.
And that just stops things like search engines from trying to index this page, which they'll try to
do, or from someone manually typing in the UFO and getting there and saying, well, there's no information
to show, so why am I even seeing this page?
So right now, because I stopped and restarted my application, which erased the session in our case,
when I tried to go there, I get a blank screen and that's not ideal.
We want something else to happen.
So let's go back to our code and I want to look at our template data go file.
And that's under internal in the folder called Where Is It?
Under Models.
It's called Template Data Dutko.
And you remember a long time ago I put in these things flash warning and error and I said, we'll be
using those later and we're going to use them later right now.
So those actually are in our template data that's passed to every single page of our site, but we're
not using them at all.
So let's go back over to the render ago, which is under the render package in the internal folder.
And let's look at this, this part where we add default data right here.
I am going to populate those three things right in here.
Every time my page is rendered, we're going to populate them and we're not going to manually type things
in.
We're going to take advantage of our session so we can do that as follows.
Tag flash.
And that will be a success message or a message, a messages that appears once and then is automatically
taken out of the session, which is exactly what we want.
We're going to pull this out of the session and we have access to our app variable right here so we
can get our session by going out session.
And no, I'm not going to use, get or put.
I'm going to use pop string, which is again built into our sessions package.
And this is a really useful one.
This will put something in the session until the next time a page is displayed and then it's taken out
automatically.
So it's the perfect place to put messages we want to send to our user.
So I'm going to put that in there pop string.
And it requires a couple of arguments.
The context ARG context.
Well, I don't do I have the context?
There it is.
Yes, I do have the context.
So ARG context and it also requires a name flash.
Since this is a flash variable, that's what I'm going to be calling it.
I'll just call a flash and I'll do the same thing for warning and the same thing for error.
So Teda Error and TDF warning and I'll call error.
Error seems like a good name and I'll call warning warning, which seems like a good name.
So those will automatically be populated any time.
I am rendering a page now back in my handlers, let's change this part that says if not OK.
And remember that this is when we're trying to pull a sessional variable named reservation out of the
session and to type cast it to models reservation.
If that doesn't work, that means one of two things.
Either there's no such and variable data at all or there's no variable called reservation in the session
or the value that's in the session that happens to be name reservation can't be cast to a model reservation,
whatever the case may be, I definitely want to take them somewhere else.
So I'll leave the log file there for now, but I'll now pass a value into our session.
Emden app session put.
And I want to put from context, I will put a value called Okkult error, because this is an error and
I'll give it a value.
And the error I want to put in there is I can't get a reservation from session, which isn't a great
error message, but it's sufficient for our purposes.
So I've now popped that value in there.
And I don't know because it's still just returning an empty page.
So I need to do an HTTP redirect.
Where shall I take the oil issue?
Topdog redirect and I'll just take them to the homepage and I'll give the status http status temporary
redirect.
This time it has to be in the 300 range for a redirect and a temporary redirect is perfect because maybe
they're going to make a reservation, maybe they'll come back here, whatever the case may be, that
is sufficient for our purposes.
So this technically should work now.
We're not going to do anything with that sectional variable yet, but let's just make sure it actually
takes us to the homepage.
So I'll stop the application, start the application that clears everything out and compiles in my changes.
And I'll try just to load this page by doing this and it takes me back.
So it did redirect me to the home page, but it didn't display the error.
So how do I get the error message displayed?
I want to use Nottie.
I want an error message to show up here displaying whatever my error message is.
Well, let's go back to base left.
Let's find that file.
Which is under my code and it's under my templates and it's called base.
So let's find that.
And remember, in this base layout file, we have a bunch of JavaScript that appears on every page.
And this is exactly the situation where I want to handle every possibility.
Every time a user is displayed a page, if there's an error message or a success message or a warning,
I want that to be displayed.
So we'll just worry about success and error for right now.
So let's put those in here and I'll put them right after these two things.
After our notify and notify Motal, I'll create another thing.
Now, what I want to have happen is when someone comes to a page and I need to display an error message,
I want to actually call success.
I want to call it error message.
So I could just do it like this notify, which is right up here.
We've already done this and notify takes two arguments.
It takes a message and a message type and the message will be whatever I get from some value.
So I'll just put a dot there right now as a placeholder.
Actually, that needs to be in quotation marks.
So it's delete this and dot and we'll get that dot populated in a minute.
And the second argument is the message type.
And in this case it happens to be an error.
Now how do I get that dot which in Goldwing template syntax means some kind of data.
I get it like this with and dot here.
And end, which is a shorthand, it says if you have some value in error, that's not empty.
Do this with it.
So it's like an estimate and that will display it if there's an error.
Now, what if I have a success message?
Well, that's stored in a value called flash.
So with Flash and this time it's type success.
So if I start my application and run it again and go back to my Web browser and manually type in the
reservation summary right there and hit it, it partly back and it gave me my error message and that's
exactly what I want to have happen.
So that's really convenient.
Now we could of course, we haven't done that, but if we do one for warning as well, can we do that?
Sure, why not?
We already have a warning so we can copy this, paste it here.
We may as well do it.
Now with Flash becomes with warning, notify, warning.
Let's just make sure that works.
It should compile.
If there's an error, the template won't compile.
So, so far so good.
So let's try it again.
Reservation summary.
And there it is.
OK, so everything compiled, so it works well.
So now I have a means of getting information to my users in the form of an unobtrusive alert.
That's really quite helpful.
And there's one more thing I want to do, and it's a really simple thing.
We've left our reservation in the session.
So when I come back here to my code and look at my handlers, what I'm talking about is this let me
just hide this.
So back in my handlers, when I post wherever that is right here, I posted to my session or I put in
my session then with the name of reservation, the value of this reservation.
Well, once the reservation is complete, that really should be taken out of the session.
So let's go back down here.
We pull it out of the session here.
We check for OK.
And after we get that, we already have the value from the session.
So we don't need it in there anymore.
So let's get rid of it and update session.
And there's got to be a method that says, get it out of here and there it is.
Remove.
So removing using our context, which is right here, and just give the name it has and that is reservation.
So with that, we can actually remove data from our reservation and that is quite helpful.
OK, that's it for this section.
In the next section, we're going to move on to testing because we talked about testing really early
in the course, but we haven't actually written any tests for our code.
That's going to be our final project.
So we'll devote the next few sessions to that.