4.2 Status Messages and Other Spit and Polish
Before moving on, I want to add a few little features that will add to the user's experience. For example, let's add status messages to our app, so that users can get feedback on completed operations.
1.Introduction1 lesson, 01:23
2.Getting Started4 lessons, 46:41
3.Managing Pages6 lessons, 1:12:31
4.User Management2 lessons, 27:37
5.Managing the Blog4 lessons, 41:51
6.Adding Extras2 lessons, 26:07
7.Implementing the Front-End3 lessons, 30:24
8.Homework Review1 lesson, 07:11
9.Conclusion1 lesson, 01:24
4.2 Status Messages and Other Spit and Polish
There are a few things that I want to do to the back-end before we move to the front-end, just a few little things to polish to make it a little bit easier for the user to use. But first we need to protect our users controller, because right now it is completely open. So we can start by doing what we did in our pages controller. So let's just copy that constructor and paste it into our users controller, that's going to at least limit the availability to users that have admin, editor, and author. But editors and authors don't need this functionality either. So what we should do is lock it down even more so that only admins, and we can do that, we have a policy called manage users that we could use inside of every method. Now if you're thinking there's got to be a better way. You are correct, because I don't want to go to every method and add that check. So instead what we can do is use our policy just like middleware. So inside of our constructor, we will say middleware. And then we will say can manageUsers. But then we need to specify, all of the other information. Anytime that we have used manageUsers, which I can only think of once. We also had to supply what type of data that we were working with. In this case, it's the user, so we say can manageUsers, and then the name of the class, User. And that is going to protect it now, we could probably get away with just using that. But we'll leave in our admin there as well. So let's go into at least protect our controller, so we can refresh we can see that that works. If we log out and login as Joe, then we are going to be unauthorized for that resource. So everything is going to be fine. So now I want to add some status messages. Anytime that we do something, we infer that something happened but we never really tell the user what happened and I would like to do that. So anytime that we create or edit, or we don't allow something, let's tell the user. Like for example, whenever we try to edit ourselves, well, we should display a message saying, hey, you can't edit yourself. So instead of our users controller, whenever we redirect back to the index, we can also include a message. So we will say with, and then we're going to flash data to the session. This is something that's good for only one request. But that's all that we need it for. So in this case, we're just going to say that we are going to redirect to users on index with this data called status. And then we just need to supply a value. So, you cannot edit yourself. There's probably a better way of phrasing that but that is going to be sufficient. And we need to do that, not just for the edits method, but also for the update just to catch those requests as well. And then when the user is actually updated, we also need to tell them that, hey, this user was updated. And in this case, we can tell them which user it was. So let's do that. We'll say user name was updated. And we could probably even add more information like, the user was updated with these roles, but that's getting a little bit too much. I mean, the first thing that comes to my mind is, yeah, they were updated with the roles, I just did it. So we'll just say that the user was updated, and that will be fine. So now we need to display that information. And we do that inside of our index view. So let's go to the index inside of our users. And let's add this before our table, so that any message is going to be right there at eye level. So, we will first of all check to see if we have this status in our session. So we will say session, we'll pass in status, and then let's also add in the and if. And then if we do have a status, then we of course just want to display that. So, we will have a div. Let's give it a class of alerts and we will also use alert info that will give it a blue background, and I don't want to use green, or red, or anything like that. I mean ideally, if there was an error we would display red. If something was successful we'll use green, but info is going to be fine. And then we will just output that session data. So we will say session status, and there we go. So let's go back, let's attempt to edit ourselves. And we see that we cannot do that, great. So let's edit Joe. We made him an editor and he's botched a few things. So he's bumped back down to author, we submit and Joe was updated. So, great. There's at least some information here for the user, but something else that we could do is add some pagination. Because, right now we just have two users. And if we're going to be a very big site and have a lot of contributors, we could have a lot of listed here. And I have no problem scrolling, some people do so, we could add some pagination. Pagination is very easy to do. So what we could do is inside of our users controller, whenever we pass all of our users, instead of using all we could say paginate. And then specify how many users that we want at a time. So well, since we only have one user, or two users, let's paginate with one. So that's whenever we view this in the browser, all we get is one. Now of course, we want to be able to navigate to the other pages. So inside of our index view, we will simply add in those links. And we can do that by saying model links. And whenever we go back, let's refresh the page. Boom, there they are, and it automatically works. We don't have to do any type of checking inside of our action method. It's just going to work. So there we go. We have our pagination. I'm going to leave this as it is, even though it's not as usable. Well, I guess we could do this. We have the pagination there, but let's bump it back to like 20 or so. And that way we'll have those two users there, and notice that the links go away as long as we don't need to paginate anything, well there's no need to show the links. So there we go. So, we are done with our users controller. Now we just need to focus on our pages. So there's several places where we can use those messages whenever we create a page, whenever we edit a page. So let's go to our pages controller. And let's look at where we can do those things. Now we can add pagination. It is just as simple to do that here as it was for our users instead of all, say paginate. Let's just go ahead and do it since we're here. Let's do 5 in this case, but for our individual user, but we'll say is pages and instead of gets, we'll call paginate, and let's do five there as well. I think we only have two or three, so we won't actually see the pagination there, but that will be okay. So when it comes to storing, we are saving, and then we are redirecting. So here we want to specify our status. So we will say status and then the page has been created. And that's fine, we could probably give a little more information like include the title or something like that. But that's okay, we'll just stick with this. So let's copy that., let's go to our update. And whenever we update, we will essentially do the same thing except that the page was updated, so updated. And is there any other place? I don't think so. Eventually, whenever we go over the destroy, well, then we'll have a message there. But that's enough. So let's take what we did inside of the index view for our users. And let's go and do that for our pages. So let's open up index. Now the placement for this, we could debate whether or not we should put it above the create link or below the create link. I'm gonna do it below, but just see what that looks like. If we don't like it, we can always change it. And then of course after the table, let's add in those links. So we we'll say I think we called this user pages. In this case, we didn't use models, and then links. So let's look at our list here. So we have pagination, that's great, everything's fine. And let's go to the about page. So we are going to edit this. This has been edited. Let's submit, that is going to update and yeah, that does not look good. So let's put the message at the top. We need some kind of break there. And I'm going to just do that. I mean we could and should do that with CSS, but the break is going to be fine. Okay, so let's do this, let's create a new. This is a new page, the URL is this, is new content. And we'll see how this looks then. That looks a lot better, we can see that the page was created. We have some break in between our message and our button. And of course we have the pagination as well. Well in the next lesson, we are going to start implementing our blog management, which is quite frankly going to be very similar to the things that we have done thus far.