Username Slugs and an MVC Sinatra app

Ready for acronyms? I just built a CMS using CRUD capabilities and an MVC app in Sinatra — a domain-specific language written in Ruby.

  • CMS — Content Management System
  • CRUD — Create, Read, Update, and Delete
  • MVC — Models, Controllers, Views.

All of that is fancy talk to say I created an app where the user is able to make and then interact with their content. This includes user authentication so that only the creator could manipulate their information.

Today is my 53rd day since joining Flatiron School — a coding bootcamp. Since starting I have created 146 repos on Github, completed 281 lessons and labs. Phew. It’s fast paced and intense to say the least, but I. am. loving. it. It’s so much fun to be so immersed in new languages and new ways of thinking. However, it can be hard to keep track of everything you are learning. Enter my idea for the Ruby Gem Tracker.

Ruby Gems are programs and libraries built in the Ruby language that make the developer’s life so much easier. The gems are pre-packaged bundles of code that are ready and waiting for developers to use to add functionality, set up directories, classes, make console manipulation easier, etc.

While gems are wonderful, it can be hard to keep track of what gem does what. What’s the difference between the sinatra gem, the sinatra-activerecord gem, and the activerecord gem?! I got tired to trying to remember which did what, so I created a program to organize this information all in once place.

Anywho, I’ve gotten sidetracked. While I like my project, I wanted to talk about username slugs.

Slugs you say? No, not slugs…Username slugs!

While, cute, I don’t mean these slugs…

Nobody wants to be known as ‘number 1639’, they’d rather show the world their very creative BettyBoop_43 username. A slug is a unique identifier that is readable and takes the place of the less readable version like an id number. People use slugs when they want to preserve the ability to see something at a glance (i.e. text) while still referring to that thing.

This wouldn’t mean much to anyone:

https://medium.com/asdkljh3458359fjalsdkfj/asdf987kjhtkajdf987-bdf5defdde16

However, medium utilizes two different slugs for my blog page:

https://medium.com/@amandawalkerbrubaker/username-slugs-bdf5defdde16

…which is so much better than referring to my blog page by a random id or my blog post by a unique but random id.

If you ever need to create your own web page that has user login capabilities, I recommend making a slug. They’re pretty simple and look great.

In my User Class I set up two different slug methods:

The username slug methods from my User Model Class

My slug method ‘strips’ away all white space if any exist before and after the username, makes sure no letters are capital (similar to how medium shows my name in the web page url), and if for whatever silly reason a user tried to create a username with a space, it would substitute that space with a dash “-”. Since we can’t have spaces in a url, this is an easy workaround.

Here’s how I used my slug methods:

Check out the GET request with the username slug as well as identifying a user by their slug from the URL rather than their ID number.

Normally with a get request the controller action would begin with

get '/users/:id' do

but mine is

get '/users/:slug'

This allows the url to show www.users/bettyboop_43 rather than showing www.users/2635.

Username slugs are a fun and easy way to engage your audience. Thanks for reading follower number 3, I mean hammertime42!

I hope you enjoyed the blog and until next time, peace.

Learn. Code. Share. Teach. Repeat.