Project

human_urls

0.0
Low commit activity in last 3 years
No release in over a year
Human URLs is an intentionally simple and straight forward way to add slugs to a Rails application.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.1
 Project Readme

Human URLs

Human URLs is an intentionally simple and straight forward way to add slugs to a Rails application.

Because of its simplicity Human URLs currently does not have an option to store a history of changed slugs. If you are looking for this functionality, check out FriendlyId.

Install

Human URLs is a Rails engine tested against Rails >= 4.1 and Ruby >= 2.0.0. To get started, add Human URLs to your Gemfile and bundle install.

gem 'human_urls'

Use

Basic Use

Human URLs stores the slugs in the DB so we'll need to generate a migration to add the slugs to the model. Generate a migration that adds the following:

class AddSlugToPost< ActiveRecord::Migration
  def change
    add_column :posts, :slug, :string
    add_index :posts, :slug, unique: true
  end
end

You can see we add a string column for the slug to be stored and then we index it since it will be unique.

Alternatively you can just use the generator to do this for you:

rails generate slug_migration Post

Once the DB is ready, include the HumanUrls::Sluggable module in your model and call the 'sluggify' method:

class Post < ActiveRecord::Base
  include HumanUrls::Sluggable
  sluggify
end

Next you'll probably want to make sure you add some validation to make sure slugs are unique:

validates_uniqueness_of :slug, case_sensitive: false

Lastly, you'll just want to make an interface on the view layer so you can create your slugs.

Auto Slugs

If you choose, you can specify a column to generate a slug from:

class Post < ActiveRecord::Base
  include HumanUrls::Sluggable
  sluggify :slug, generated_from: :title
end

This will automagically generate a slug from the title, if no slug is specified.

In the Controller

Finding your model in the controller will be a little different. Instead of finding the model by the ID, you'll need to find the model by the slug:

def show
  @post = Post.find_by_slug!(params[:id])
end