Agile Web Development

Build it. Launch it. Love it.

Friendly

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

Using FriendlyId, it's easy to make your application use URL's 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, scoped slugs, reserved words, custom slug generators, and excellent Unicode support. For complete information on using FriendlyId, please see the {http://norman.github.com/friendly_id/file.Guide.html FriendlyId Guide}.

FriendlyId is compatible with Rails 2.2.x - 3.0.

Rails Quickstart

gem install friendly_id

rails my_app

cd my_app

# add to Gemfile
gem "friendly_id", "~> 3.0"

rails generate friendly_id
rails generate scaffold user name:string cached_slug:string

rake db:migrate

# edit app/models/user.rb
class User < ActiveRecord::Base
  has_friendly_id :name, :use_slug => true
end

User.create! :name => "Joe Schmoe"

./script/server

GET http://0.0.0.0:3000/users/joe-schmoe

Docs, Info and Support

Bugs:

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

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

  • Stack trace and error message.
  • Version information for FriendlyId, Rails and Ruby.
  • Any snippets of relevant model, view or controller code that shows how your 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.

Credits:

FriendlyId was created by Norman Clarke, Adrian Mugnolo, and Emilio Tagua.

Copyright (c) 2008-2010, released under the MIT license.

Vitals

Home http://norman.github.com/friendly_id
Repository git://github.com/norman/friendly_id.git
License Rails' (MIT)
Rating (19 votes)
Owner Norman Clarke
Created 18 January 2008

Comments

  • Avatar
    20 January 2008

    On Scenario 1, just add

    hasfriendlyid :title

    (where :title is the column you want to be friendly)

  • Avatar
    21 January 2008

    Thanks for the feedback, Marcus. I have corrected the documentation.

  • jc
    28 March 2008

    is there an easy way to make the slug write to the same table as my model and i'd just add a field to that model for it?

  • josh deeden
    18 May 2008

    this plugin has been moved to github.

    http://github.com/norman/friendly_id/tree/master

  • Avatar
    John Austin
    17 June 2008

    I followed your tutorial but I got this error:
    undefined method `blog_url' for #<BlogController:0x237d258>

    Also in the view I have the link pointing to the id of the post:
    <%= link_to @post.title, :action => "show", :id => @post.id %>
    Could you please help me fixing this problem since I'm a newbie to Rails?

  • Avatar
    Brian
    10 July 2008

    I'm probably doing something silly, but when I do

    rake friendlyid:makeslugs MODEL=user

    I get

    rake aborted! wrong constant name user

    Any ideas?

  • Avatar
    Brian
    10 July 2008

    In case anyone else has the same problem, I figured it out. The model name has to be capitalized, as in "User"

    Also if you have already defined a slug method in your model you have to remove it.

  • Avatar
    2 November 2008

    Hi, just wanted say thanks for creating this plugin... works great with my existing app and took me less than five minutes to implement. Used it on jettyrae.com. Thanks again!

  • Avatar
    14 January 2009

    Just a short comment to anyone deploying friendlyid and use the stripdiacritics option on Heroku.com (worked locally though). I did this and for some reason it didn't strip swedish characters "åäö" to "aao". I temporarily fixed it by adding a few gsub commands to the normalize method in slug.rb like:

    s.gsub!(/å/, 'a') s.gsub!(/ä/, 'a') s.gsub(/ö/, 'o')

Add a comment