Build Status Code Climate Inline docs

GETTING HELP

Please ask questions on Stack Overflow using the "friendly-id" tag. Prior to asking, search and see if your question has already been anwered.

Please only post issues in Github issues for actual bugs.

I am asking people to do this because the same questions keep getting asked over and over and over again in the issues.

VERSION NOTE

Rails 4:

The master branch of this repository contains FriendlyId 5, the current stable version. 5.x is the only version that is compatible with Rails 4.

Rails 3:

If you wish to use this gem with Rails 3.1 or 3.2 you must use FriendlyId 4, which is the previous stable release. Please see the 4.0-stable branch.

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:

http://example.com/states/washington

instead of:

http://example.com/states/4323454

FriendlyId Features

FriendlyId offers many advanced features, including: slug history and versioning, i18n, scoped slugs, reserved words, and custom slug generators.

What Changed in Version 5.0

As of version 5.0, FriendlyId uses semantic versioning. Therefore, as you might infer from the version number, 5.0 introduces changes incompatible with 4.0.

The most important changes are:

  friendly_id :foo, use: :slugged # you must do MyClass.friendly.find('bar')
  # or...
  friendly_id :foo, use: [:slugged, :finders] # you can now do MyClass.find('bar')
  class Restaurant < ActiveRecord::Base
    extend FriendlyId
    friendly_id :slug_candidates, use: :slugged

    # Try building a slug based on the following fields in
    # increasing order of specificity.
    def slug_candidates
      [
        :name,
        [:name, :city],
        [:name, :street, :city],
        [:name, :street_number, :street, :city]
      ]
    end
  end
  restaurant.friendly_id # joes-diner
  restaurant.name = "The Plaza Diner"
  restaurant.save!
  restaurant.friendly_id # joes-diner
  restaurant.slug = nil
  restaurant.save!
  restaurant.friendly_id # the-plaza-diner

You can restore some of the old behavior by overriding the should_generate_new_friendly_id? method. * The friendly_id Rails generator now generates an initializer showing you how to do some common global configuration. * The Globalize plugin has moved to a separate gem (currently in alpha). * The :reserved module no longer includes any default reserved words. Previously it blocked "edit" and "new" everywhere. The default word list has been moved to config/initializers/friendly_id.rb and now includes many more words. * The :history and :scoped addons can now be used together. * Since it now requires Rails 4, FriendlyId also now requires Ruby 1.9.3 or higher.

Upgrading from FriendlyId 4.0

Run rails generate friendly_id --skip-migration and edit the initializer generated in config/initializers/friendly_id.rb. This file contains notes describing how to restore (or not) some of the defaults from FriendlyId 4.0.

If you want to use the :history and :scoped addons together, you must add a :scope column to your friendly_id_slugs table and replace the unique index on :slug and :sluggable_type with a unique index on those two columns, plus the new :scope column.

A migration like this should be sufficient:

add_column   :friendly_id_slugs, :scope, :string
remove_index :friendly_id_slugs, [:slug, :sluggable_type]
add_index    :friendly_id_slugs, [:slug, :sluggable_type]
add_index    :friendly_id_slugs, [:slug, :sluggable_type, :scope], unique: true

Docs

The most current docs from the master branch can always be found here.

Docs for older versions are also available:

The best place to start is with the Guide, which compiles the top-level RDocs into one outlined document.

You might also want to watch Ryan Bates's Railscast on FriendlyId, which is now somewhat outdated but still relevant.

Rails Quickstart

rails new my_app
cd my_app
# Gemfile
gem 'friendly_id', '~> 5.0.0' # Note: You MUST use 5.0.0 or greater for Rails 4.0+
rails generate friendly_id
rails generate scaffold user name:string slug:string:uniq
rake db:migrate
# edit app/models/user.rb
class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
end

User.create! name: "Joe Schmoe"

# Change User.find to User.friendly.find in your controller
User.friendly.find(params[:id])
rails server

GET 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)

Benchmarks

The latest benchmarks for FriendlyId are maintained here.

Bugs

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

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

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 info 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-2014 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.