Summary

GitHub Issue

Users want a custom URL for their profile. So instead of visiting /profiles/2 they can visit something like /profiles/Gustavo.

Proposals

Profile Slugs

Proposal: New Field for Slugs

There will need to be a new profile field to hold the custom “slug”

def change
  add_column :profiles, :slug, :string
  add_index :profiles, :slug, unique: true
end

The uniqueness is necessary because no two users should have the same slug. The unique index enforces uniqueness at the db level and gives better performance on reads.

Custom URL Routing

Proposal 1: Use Builtin Rails Techniques

We can roll our own system here. The profiles controller can look up a profile by a slug field with code like:

Article.find_by!(slug: params[:id])

Pros

  1. Simple and straightforward
  2. No new dependencies

Cons

  1. Not as robust as a gem.

Proposal 2: FriendlyId Gem

friendy_id gem