• Stars
    star
    6,088
  • Rank 6,280 (Top 0.2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 16 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.

Build Status Code Climate Inline docs

FriendlyId

For the most complete, user-friendly documentation, see the FriendlyId Guide.

FriendlyId is the "Swiss Army bulldozer" of slugging and permalink plugins for Active Record. It lets you create pretty URLs and work with human-friendly strings as if they were numeric ids.

With FriendlyId, it's easy to make your application use URLs like:

https://example.com/states/washington

instead of:

https://example.com/states/4323454

Getting Help

Ask questions on Stack Overflow using the "friendly-id" tag, and for bugs have a look at the bug section

FriendlyId Features

FriendlyId offers many advanced features, including:

  • slug history and versioning
  • i18n
  • scoped slugs
  • reserved words
  • custom slug generators

Usage

Add this line to your application's Gemfile:

gem 'friendly_id', '~> 5.5.0'

Note: You MUST use 5.0.0 or greater for Rails 4.0+.

And then execute:

bundle install

Add a slug column to the desired table (e.g. Users)

rails g migration AddSlugToUsers slug:uniq

Generate the friendly configuration file and a new migration

rails generate friendly_id

Note: You can delete the CreateFriendlyIdSlugs migration if you won't use the slug history feature. (Read more)

Run the migration scripts

rails db:migrate

Edit the app/models/user.rb file as the following:

class User < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: :slugged
end

Edit the app/controllers/users_controller.rb file and replace User.find by User.friendly.find

class UserController < ApplicationController
  def show
    @user = User.friendly.find(params[:id])
  end
end

Now when you create a new user like the following:

User.create! name: "Joe Schmoe"

You can then access the user show page using the URL http://localhost:3000/users/joe-schmoe.

If you're adding FriendlyId to an existing app and need to generate slugs for existing users, do this from the console, runner, or add a Rake task:

User.find_each(&:save)

Options

:allow_nil

You can pass allow_nil: true to the friendly.find() method if you want to avoid raising ActiveRecord::RecordNotFound and accept nil.

Example

MyModel.friendly.find("bad-slug") # where bad-slug is not a valid slug
MyModel.friendly.find(123)        # where 123 is not a valid primary key ID
MyModel.friendly.find(nil)        # maybe you have a variable/param that's potentially nil
#=> raise ActiveRecord::RecordNotFound

MyModel.friendly.find("bad-slug", allow_nil: true)
MyModel.friendly.find(123, allow_nil: true)
MyModel.friendly.find(nil, allow_nil: true)
#=> nil

Bugs

Please report them on the Github issue tracker for this project.

If you have a bug to report, please include the following information:

  • Version information for FriendlyId, Rails and Ruby.
  • Full stack trace and error message (if you have them).
  • Any snippets of relevant model, view or controller code that shows how you are using FriendlyId.

If you are able to, it helps even more if you can fork FriendlyId on Github, and add a test that reproduces the error you are experiencing.

For more inspiration on how to report bugs, please see this article.

Thanks and Credits

FriendlyId was originally created by Norman Clarke and Adrian Mugnolo, with significant help early in its life by Emilio Tagua. It is now maintained by Norman Clarke and Philip Arndt.

We're deeply grateful for the generous contributions over the years from many volunteers.

License

Copyright (c) 2008-2020 Norman Clarke and contributors, released under the MIT license.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

babosa

A library for creating slugs. Babosa is an extraction and improvement of the string code from FriendlyId, intended to help developers create similar libraries or plugins.
Ruby
526
star
2

disqus

A Ruby library for the Disqus commenting API and Javascript widgets.
Ruby
249
star
3

telescope

A highly customizable test library for Lua that allows declarative tests with nested contexts.
Lua
159
star
4

haml-scaffold

Rails scaffold generator that outputs Haml and better functional tests.
Ruby
114
star
5

squirm_rails

Easily use and manage Postgres stored procedures with Active Record.
Ruby
73
star
6

friendly_id-globalize

Globalize support for FriendlyId
Ruby
65
star
7

ambry

Ambry is a database and ORM replacement for (mostly) static models and small datasets. It provides ActiveModel compatibility, and flexible searching and storage.
Ruby
56
star
8

lua-haml

Haml for Lua
Lua
52
star
9

hello-lua

A demo of how to make simple C modules for Lua
C
33
star
10

squirm

A library that simplifies working with Postgres stored procedures.
Ruby
18
star
11

grackle

A static blog generator written in Lua
Lua
18
star
12

lua-postgres

A basic Postgres driver for Lua
C
16
star
13

has_image

A lightweight and hackable library for attaching images to ActiveRecord models.
Ruby
16
star
14

phonenumber

Allows parsing and formatting of phone numbers
Ruby
16
star
15

luacov

LuaCov is a simple coverage analyzer for Lua code. (fork of official CVS repo)
15
star
16

hops

A lightweight, pluggable web framework for Lua
Lua
15
star
17

lua-devtools

An irb-workalike for Lua, and a command-line debugger with readline support.
Lua
15
star
18

tlua

A simple task runner for Lua - now abandoned because I think it's just easier to use plain old Makefiles.
Lua
14
star
19

spanish

Linguistic utilities for working with Spanish words.
Ruby
12
star
20

enc

Notes and slides from my Encodings talk at RubyConf Brasil 2010
Ruby
12
star
21

nearby

Quick and easy geocoding using Geonames.org data and TokyoCabinet.
Ruby
10
star
22

nt54

Argentine phone number parsing, validating, formatting and meta-info
Ruby
10
star
23

active_record_random

Monkey patch to ActiveRecord to allow :order => :random that works the same for MySQL, SQLite and Postgres.
9
star
24

fatalistic

Table locking for Active Record
Ruby
9
star
25

yourbugreportneedsmore.info

The yourbugreportneedsmore.info website
HTML
8
star
26

friendly_id_manual_slug_demo

A Rails 3 app showing how to manually control FriendlyId slugs
Ruby
7
star
27

mongrel2_wsapi

In-progress Lua WSAPI adapter for Mongrel2
Lua
6
star
28

base-site-generator

The current base layout and helpers I'm using to scaffold quick projects. Feel free to use if and as you wish.
Ruby
6
star
29

squirm_model

Model API for Squirm
Ruby
5
star
30

micro_factory

Minimal factories for Active Record.
Ruby
4
star
31

utf8_utils

Utilities for cleaning up UTF8 strings.
Ruby
4
star
32

phonology

Phonology utilities for Ruby
Ruby
3
star
33

dotfiles

My dotfiles
Vim Script
2
star
34

wsapi_test

Mock connector for unit testing WSAPI apps. TEMP. Go here instead: http://github.com/norman/wsapi
Lua
2
star
35

hash_formatter

Hash Formatter is a library that formats Ruby hashes for code editors.
Ruby
2
star
36

gem_init

My Ruby Gem initialization biolerplate
Ruby
2
star
37

luaargentina

luaargentina.org website
2
star
38

luadns-zones

My DNS zones hosted on LuaDNS.
Lua
1
star
39

friendly_id_join_test

Temporary debugging repo, don't follow
Ruby
1
star
40

haml_textarea_bug

Demonstrates a bug in Haml
Ruby
1
star
41

vim-files

My vim files
Vim Script
1
star
42

friendly_id_sequel

An adapter which allows you to use FriendlyId with Sequel::Model
Ruby
1
star