Customizing Rails Routes
During my second module at Flatiron School, I had the pleasure of working with a group member who was willing to integrate human-readable slugs that would ultimately make our project look more clean/presentable.
Yes, a slug.
At first, I was confused as to what a slug really was. Were they referring to a “shell-less terrestrial gastropod mollusk”, or is there something more?
A slug can be defined as a part of the URL, which identifies the page that a user is on through human-readable keywords. Although not always, it’s usually at the end of the URL. Sometime’s a file name or the name of a resource that is currently being accessed.
An example would be https://www.facebook.com/(your_name). In rails having a show page of a user would normally end with their userID, but in this example, the user would instead be identified but a custom identifier that they have set themselves or could be custom generated through different attributes that the user was created with.
A way to integrate slugs into your next Rail’s project would be through a gem called “FriendlyID”
FriendlyID defines itself as the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord.
What FriendlyID can do for you is turn a link like :
into a link like:
FriendlyID is extremely easy to set up, and here are the simple steps to go about making your URL's more friendly.
Make sure you’re including the Gem Friendly_ID in your gemfile, note that if you’re running on rails 4.0+ the version of friendly_id has to be 5.0.0 at least!
gem 'friendly_id', '~> 5.2.4'
Next, we can proceed to add slugs to any model in your project that needs it!
rails g migration AddSlugToUsers slug:uniq
Like I stated once before, slugs can be added to any existing model!
rails generate friendly_id
This creates a new file which essentially contains configuration settings for FriendlyID (Which we won’t touch at this point!)
After running your migrations there’s still a few more steps!
class User < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
endclass UserController < ApplicationController
def show
@user = User.friendly.find(params[:id])
end
endUser.find_each(&:save)
What we’re doing here is telling the user class here is the custom URL slug that we want to be shown is the name of the user!
We now have to do User.friendly.find(params[:id]) to search for our user instead of .find to return the correct User.
The most important last step is to find_each(&:save).
Any new users that are created will automatically have that unique slug attribute and automatically have their new custom route, however, any previously existing users will not. Resaving them through this method will assign them their custom slug!
But wait there’s more!
resources :users, param: :slug
Editing your routes in this fashion completely removes the ability to search through users using numbers!
Custom Slugs!
The last trick to having real custom slugs would be to make your own!
friendly_id :slug_candidates, use: :slugged
Instead of using a single attribute of the object, we can instead create a custom getter function in our model and use the value of that to create our slug!
def slug_candidates
["#{self.name}-#{self.age}-#{self.bio}"]
end
Adding this into our user model will instead generate a link that looks like
FriendlyID is an extremely powerful and convenient way to create more human-readable pages for your users! It doesn’t take long to implement at all and it also helps in ensuring that users cant jump from one resource to another! (before proper implementation of auth)
Now get out there and start making your own custom slugs!
REFERENCES: