FriendlyId works with ActiveRecord, and activerecord is available for Sinatra. Follow this blog tutorial http://danneu.com/posts/15-a-simple-blog-with-sinatra-and-active-record-some-useful-tools/ to see how to get that going. What I did after that was put friendly_id in my gemfile, ran
bundle
rake db:create_migration NAME=add_slug_to_posts
Edit the newly created migration to look like
class AddSlugToPosts < ActiveRecord::Migration
def change
add_column :posts, :slug, :string
add_index :posts, :slug, unique: true
end
end
then run
rake db:migrate
AFTER that I required it in my app.rb file (require 'friendly_id'), and made sure I put these two lines at the top of my Post class
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
...
end
Wherever I referenced :id in the url, params hash or anywhere, whether it be in app.rb or the links in views, I changed it to :slug. The slug is a unique identifier so it works fine. I already had some posts in the database, so I manually input slugs just by running tux then putting
a = Post.find(1)
a.save
(and changing the number) for each entry, and friendly_id automatically creates the slug. It will also create all the slugs on the creation of any new post. Beauty!
btw my versions are
activerecord = 3.2.13
friendly_id = 4.0.9
ruby = 1.9.3