Ruby on Rails #52 gem Friendly ID: The Complete guide. Generate readable URLs like a PRO

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello today we are going to go through a really interesting problem ruby on 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 of the post by default instead of seeing an seo and human readable friendly 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 each record in our database and we don't want to show the id why should we 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 are 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 ids and showing human readable urls but this one is the most popular and widely understood solution so you can see that there are 30 000 public applications that are using so it is extremely widely popular now how do we install friendlinesd and use it so here is again an example instead of showing the states 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 we are going to add a migration adds log to well to posts so the slog is the human readable id that the post is going to have i will say i'll stop the server and say rails generate migration the slog to posts and the slog should be unique now slog is the database attribute inside the table where we're going to keep our human readable url let's have a look at this migration migrate adds log to posts here you see they are in the field and they're saying that it 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 run 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 ids logs now they're going to come back to this later we don't need to try to bay so i'm just going to remove this migration we are going to go first of all with a very lean approach to installing friendly id if you don't need this additional complexity with an additional database table in our application yet we are just going to add the slug to our posts and also i can remove friendly id from our initializers because we are not overriding it yet so okay we can run rails to be migrate 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 out of 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 url should be taken out of the title so should be generated based on the title of the post let's see if it works rails server now i will go to pose and create a new post i'll say how to use friendly id some kind of content and create a post and it says couldn't find post with id how to use friendly id okay maybe i should restart the server right oh 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 params so i would go to controllers post controller and here inside post we would say post dot friendly dot find params id and if i refresh and if i also start the server you see now we have a friendly url let's try with the notepost let's say installing ruby on rails and you see the 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 i'll just comment this out now so you see i've removed this friend from the controller and inside our post ob we can say that we are using friendly id for title use slogged and also use finders now i will refresh and here when we add this use finders it will automatically kind of add this friendly to our set post so we don't have to do anything inside our controller we just 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 opened the record and here you see we see the id of the post but not the friendly url now to update old records we would run a command like a user will 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 button and we can say p dot friendly id and here we see the friendly id of the first post let's say p equals post find 13 p dot friendly id so here you see the friendly ideas for all the old posts have been generated based on their post titles so seems to work file yes and actually we can also find the pos not only by the id now but we can find the post by the friendly id let's try pose dot find and inside i will add this okay and here you see we found the post by the friendly id now if what if we remove these finders from our friendly id inside the model let's see i will save rails console once again and i will say post find install ruby on rails you see it doesn't work because we've removed finders from our model but if we type pose dot friendly dot 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 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 exit the console and see what else friendly id can offer us now here you can see in the description that it says that currently ids as this army bulldozer for using the for making the urls friendly and advise itself 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 if two posts have the same title what will happen here we have uh let's create two pose 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 after votes it has an additional automatically generated hash so this way it allows us to have multiple posts for the same title but new posts would have some kind of additional hash afterwards and what everyone don't want to have some kind of additional hash afterwards well what if we 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 id let's go to fiki and go to this guide there is such a thing as slog 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 see if we can use the title and name the title and content of the post if the title is already taken so we would add something like death slug candidates and we would have the title and afterwards it would 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 try once again okay it didn't seem to work either let's see maybe we are 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's still dancing to work now did i definitely say slug candidates here i'll try 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 slug later on once again i will go and create a post with the same title and some magic content and create the post let's have a look at the url so you see here we have both the title and the content so this is how you can use this option for slack candidates looks good doesn't it now what if we have a case when we create a post so the post has the title and we want to change the title of the post what will happen i go to edit same title one two three update the post and does the slog change no it doesn't you see the slack hasn't changed but what if we actually want the slot to change when we change the title of the post well we would add another option we would add an option as 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 generate new friendly id for example if the title has changed let's see if it works i will go and edit the post and let's say ubuntu 3 abc i update the post and let's have a look at the title of the post you see it has changed to the same title one two three abc so this way we've updated the title of the post and if we go just same title under three you see we have nothing there is no such record but there can be a problem what if a title of the post changes and there were previous links in different places to the previous title of the post so people would have a 404 no such url error well we don't want people to be redirected to our posts and they just get an error that something is not found we would want old urls to redirect to new urls and this web would use the friendly id option named history now this is a really powerful option and this is where we are going to need this additional database table so we are 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 ids logs so a new database table where we keep all the history all the previous urls to all our posts to all our users and all our other records that are using friendly id so they're gonna run railsdb migrate and we'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 avoiding propose then slugs 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 copy this i will go and change the url so i will edit the post with history abc i update the post you see the url has changed and now i'm going to the previous url just with history i refresh the page and it works so you see the old url to 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 all these methods inside the console let's just try rails console let's say p1 equals post.great title will be uh using friendly id let's say pivan dot 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 friendly idea of the second post i will go and say p2 dot friend lady and here you see we have the title and the content because we are using slide candidates so here's a perfect implementation of friendly id and that's basically it you see it has a lot of options and you don't always need to use history you don't always need to use slug 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 regenerate it and again you don't always need to add this additional database uh table you don't always need to add this additional history complexity based on your application so that's basically it that's how you can use friendly id that's this um knife for generating urls in a ruby on rails application so thanks for being with me and have a nice day
Info
Channel: SupeRails
Views: 340
Rating: undefined out of 5
Keywords: ruby, rails, ruby on rails, tutorial, programming
Id: 6orj2qU6JdA
Channel Id: undefined
Length: 18min 43sec (1123 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.