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, 45:12
3.Managing Pages6 lessons, 1:12:31
4.User Management2 lessons, 27:40
5.Managing the Blog3 lessons, 42:25
6.Adding Extras2 lessons, 25:48
7.Implementing the Front-End3 lessons, 32:10
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 backend before we move to the frontend, 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 user's 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 UsersController. 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 ManageUsers 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 that's going to at least protect our controller. So we can refresh, we can see that that works. If we log out and log in as Joe, then we are going to be unauthorized for that resource. So everything's going to be fine. So now, I want to add some status messages. Any time 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 any time that we create or edit, or we don't allow something, let's tell the user. For example, whenever we try to edit ourselves, well, we should display a message saying, hey, you can't edit yourself. So, inside of our users controller, whenever we redirect back to 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're going to redirect to users.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 edit 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 user was updated with these roles. But that's getting a little bit to much. I mean, the first thing that comes to my mind is, yeah, they we're 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'll say session, we'll pass in status, and the let's also add in the end-if. And then, if we do have a status, then we of course just want to display that. So we'll have a div. Let's give it a class of alerts, and we will also use alert info that will give it a blue back ground. 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 has 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 in. 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. Well, since we only have one user or two users, let's paginate with one so that 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 is even though it's not as usable. Well, I guess we could do this. All right, we have the pagination there, but let's map 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, what we'll say is pages. And instead of get, we'll call paginate. And let's do 5 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. Let's 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 will say, I think we called this use, or 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, 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.