episode source code:
https://github.com/corsego/52-friendly-id/commit/00989cb4dc0a14409e067220707e24498208bd4e
gem source:
https://github.com/norman/friendly_id
official docs:
http://norman.github.io/friendly_id/file.Guide.html
#52 gem Friendly ID: The Complete guide. Generate readable URLs like a PRO
01/09/2021
Transcript
Hello. Today we're going to go through a really interesting problem, rub rails. So if I go to my posts and create a new post, whatever the title is, here it is, you see everybody sees the ID or the post by default. Instead of seeing an SEO and human readable friendly, uh, URL to the post, everybody sees just the id and the person can also know how many posts they actually have inside our application. So this way, by default, we show the ID of uh, each record in our database and we don't want to show the id. Why should they tell everybody in the world how many records we have in our database? Instead, we want our URLs to be SEO and human readable friendly. So to do this, we're going to use the most popular solution in ribbon rails for this, the gem friendly id. Now there are actually a few different gems for obfuscating uh, IDs and uh, showing human readable URLs. But this one is the most popular and uh, widely understood solution. So you can see that there are 30,000 uh, public applications that are using. So it is extremely wildly popular. Now, how do we install frontland D and use it? So here's again an example. Instead of showing the like state's just ID or whatever, we can show a human readable one. So going here to the gem, we are going to add this gem to our gem file. Okay, and what is next? Yeah, of course we'll run bundle and then they're going to add a migration, add SL to well to posts. So the slog is uh, the human readable idea that the post is going to have. I will say, uh, I'll stop the server and say, rails generate migration. Add the slog to posts and the slog should be unique. Now slog is the database attribute inside the table where they going to keep our human readable. URL. Let's have a look at this migration migrate at slot to post. Here, you see they're adding in the field and they're saying that it uh, should have a unique index. Okay? And next it tells us that we can run rails, generate friendly id. Let's see what happens if we're on this command. So you see it creates an initializer and it creates an additional migration. And what does this migration say? So it suggests us to create an additional database table with friendly ID logs. Now they're going to come back to the Slater. We don't need to try to be, so I'm just going to remove this migration. We're going to go first of all with a very lean approach to install friendly id. If you don't need this additional complexity with an additional database stable in our application yet they're just going to add the slog to our post. And also I can remove print the ID from our initializers because we are not overriding it yet. So, okay, we can run rails to be migrated and let's do that. And it says, so we can add the friendly ID to our model to say that we want to actually use it. So we would add something like this to our posts. Let's go to app models post. And here we would add something like extend friendly id, friendly id, uh, well friendly ID would be taken outta the title. We don't actually need this here. So we are telling the application that inside the post model we are using the extension friendly id and that that slog, this friendly QRL should be taken out of the title. So it should be generated based on the title of the post. Let's see if it works Real server. Now I will go to post and create a new post. I'll say how to use friendly id, some kind of content and create a post. And uh, it says couldn't find post with id, how to use friendly id. Okay, maybe I should restart the server. Re ah, no I shouldn't. Okay, here is the problem. Couldn't find post with id, how to use friendly id because we should actually tell our controller that we should find the post, not by the regular ID but by the friendly id. So we would need to go to our controller and add this friendly to our pres. So I would go to controllers post controller, and here inside post we would say Post friendly, find PREMs id and if I refresh and if I also start the server, you see now we have a friendly URL. Let's try with remote post, let's say installing Ruby on rails. And you see that friendly URL has been generated. So it works. Now if you don't want to add the this friendly to your controller, instead you can try saying something like, uh, I'll just command this out now. So you see I've removed this friendly from the controller and inside outpost a B we can say that we are using frontlined for title use slog and also use finders. Now I will refresh and here when we add this use finders, it'll automatically kind of add this uh, friendly to our set post. So we don't have to do anything inside our controller. We just, uh, added this friendly through our model. And you see everything works. Now what if we check some kind of old record? So here are some records that have been previously created. I open the record and here you see, we see the idea of the post, but not the friendly URL. Now to update all records, we will drawn recommend like, uh, uh, user overall post find each and save. Let's try this Rails console post, find each and save. Okay? And let's check in the console P equals post find with the ID one. And we can say P, do friendly id. And here see the friendly ID of the first post. Let's say P equals post find 13 P friendly id. So here you see, uh, the friendly IDs for all the old posts have been generated based on the post titles. So seems to work well. Yes. And actually we can also find the post not only by the ID now, but we can find the post by the friendly id. Let's try post dot find. And inside I will add, uh, this. Okay, and here you see we found the post by the friendly id. Now if, uh, what if they remove this, find us from our friendly ID inside the model, let's see, I will say rails console once again. And I will say post find install rails. You see it doesn't work because we've removed finders from our model, but if you type post friendly, do find you see it works. So you should have this friendly inside your console. If you don't have it in your model, you should have it in your uh, con controller if you don't want to use, uh, finders inside your model. And that's basically it for this finder method. Okay, going back, let's, uh, exit the console and see what else friendly ID can offer us. Now here you can see in the description that it says that friendly ID is as Swiss Army bulldozer for using the, for making the URLs friendly and ol because it has a lot of different extensions. Now we've covered just the basic usage. And now let's try something a bit more, uh, complicated. So let's see, let's uh, see two posts have the same title. What will happen? You have, uh, let's create two posts. So same title, this post, and let's create the post. And I will create one more post for the same title. So you see we've created the post and it has the same title and afterwards it has an additional automatically generated hash. So this way it allows us to have multiple posts for the same title, but new post would have some kind of additional hash afterwards. And uh, what if you want don't want to have some kind of additional hash afterwards. Well, what if you want to have some kind of part of the content of the post afterwards? Let's, uh, try. Now if we go into the deeper documentation of friendly, let's go to Vicky and go to this guide. There is such a thing as slug, uh, candidates. So candidates basically, if the name is already taken, then we can also use the name and city and so on. Let's try to use slug candidates and uh, see if we can use the title and name, uh, the title and content of the post if the title is already taken. So we would add something like deaf slug candidates And we would have the title. And afterwards we'll have the title and the content. Okay, let's see if it works. So once again, I'll just in case restart the server and I will create a new post with the same title and add some kind of content, some content. Okay, I press create post and let's see. Okay, it didn't seem to work. So we don't have the content inside our post title. Let's, uh, try once again. Okay, it didn't seem to work either. Let's see, maybe they're missing something. Possibly the second part should also be a stream. Okay, I understood what my problem was. So instead of using title for friend lady, I should actually use the Slack candidates right here. So let's try on scam and see if it works. I will go back and create an additional post with the same title. And let's see, I create the post and this time I'm using Slack candidates inside Friendly id. And let's see. Okay, it still didn't seem to work. Now did I definitely say Slack candidates? Here I'll try it once again. And it still didn't work. Now I didn't talk this time because the content was blank, so that's why it was generating just a random slide later on. Once again, I will go and create a post of the same title and some magic content and create the post. Let's have a look at the URL. So see here we have both the title and the content. So this is how you can use this option for slug candidates. Looks School doesn't it. Now what if we have a case, then we create a post. So the post has a title and we want to change the title of the post. What will happen? I go to edit, same title, one to three, update the post and does the slack change? No, it doesn't. You see the slack hasn't changed. But what if we actually want the slack to change, then we change the title of the post. Well, we would do add another option. We would add an option as uh, should generate new friendly id. And yeah, we can say that it should be either true or false or conditionally true. So we would say that yes, it should uh, generate new friendly id. For example, if the title has changed, let's uh, see if it works. I will, uh, go and edit the post and let's say 1, 2, 3 A, B, CI update the post. And let's have a look at the title of the post. You see it has changed to same title of 1, 2, 3, A, B, C. So this way we've updated the title of the post. And if you go just the same title, one to three. You see we have nothing. There is no such record, but there can be a problem. What if, uh, a title of the post changes and uh, there were previous links in different places to the previous title of the post. So people would have, uh, 4 0 4, uh, no such URL error. Well, we don't want people to be redirect to our posts and uh, just get an error that something is not found. We would want, uh, old URLs to redirect to new URLs. And this is where would use the friendly ID option named History. Now this is a really powerful option and uh, this is where we are going to need this additional database stable. So they're going to keep all the past friendly ideas of all the posts and all other records inside our database and link them to the existing URLs. Now let's see how it works. I'm going to actually run this migration rails, generate friendly id. Let's do that. And here you see it's adding this great friendly ID logs. So new database table where we keep all the history, all the previous uh, URLs to all our posts, to all our users and all our other records that are using friendly id. So they're gonna run Rails to be migrate and they're going to say that we will also use History. Now we can also see this option inside our official guides. So again, it's avoidable poles and SLS change. So we are going to add history and let's see if it works. So I will start the server rails S okay, I'll restart the console rails s go to some kind of post. I'll create actually a new post. Let's name it with history. Okay? And now I will change the, I will go to this. I will go and change the URL. So I will edit the post with history A, B, C, I update the post. You see that URL has changed and now I'm going to the previous URL just with history. I refresh the page and it works. So see that all your elder, the post works though, it is the same post And this is fantastic. So here is an example of using the history and actually we can also access, uh, all these methods, uh, inside the console. Let's just try Rails console. Uh, let's say B one equals post Do great title. It'll be, uh, using friendly id, let's say P one friendly id. Here it is, let's create another post with the same title. And let's also add some content. So content will be whatever. And let's check the front lady of the second post. I will go and say bit two friendly id. And here you see we have the title and the content because we're using Slack candidates. So here's a perfect implementation of, uh, friendly id and that's basically it. You see it, uh, has a lot of options and uh, you don't always need to use history. You don't always need to use Slack candidates. And in many cases you can just have one URL that is generated on the creation of the post and you don't need to re generate it. And uh, again, you don't always need to add this additional database, uh, table. You don't always need to add this additional history or complexity based on your application. So that's basically it. That's how you can use friendly ideas, this, um, knife for generating URLs in Rubian Rails application. So thanks for being with me and uh, have a nice day.
0