EncodedId is a Ruby gem that helps developers to obfuscate numerical or hex IDs in URLs while still keeping them human-friendly. It supports encoding multiple IDs at once, as well as slugged IDs (eg
my-slug--<id>) and custom alphabets for the encoded strings. It also provides a profanity limitation and the ability to split the encoded string into groups of letters for improved readability.
coder = ::EncodedId::ReversibleId.new(salt: my_salt) coder.encode(123) # => "p5w9-z27j" coder.encode_hex("10f8c") # => "w72a-y0az"
The Rails gem provides a mixin for ActiveRecord models that allows you to easily add encoded IDs to your models.
class User < ApplicationRecord include EncodedId::WithEncodedId end User.find_by_encoded_id("p5w9-z27j") # => #<User id: 78>