Creating Slugs

We want our videos to have a unique URL-friendly identifier, called a slug. This approach lets us have a unique identifier that will build URLs that are friendlier to people and search engines. We need to create the slug from the title so we can represent a video titled Programming Elixir as a URL-friendly slug, such as 1-programming-elixir, where 1 is the video ID.

The first step is to add a slug column to the database:

 $ ​​mix​​ ​​ecto.gen.migration​​ ​​add_slug_to_videos

We generate a new migration. Remember, your name will differ based on the timestamp attached to the front of the file, but you can find the new file in the priv/repo/migrations directory. Let’s fill it in like this:

Get Programming Phoenix 1.4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.