Automatically generate two-word slugs in Ruby on Rails

28
0

This snippet ensures every article gets a unique, readable slug based on its title, keeping it short and pronounceable. It grabs the first two words of the title and appends a 4-character random string to prevent duplicates. The before_validation callback runs only when creating a new article, and the controller fetches articles by slug instead of an ID—great for SEO!

Martin Sojka, Maker of CodeSnips