FriendlyId For Friendly URL

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 Active Record 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/2

Rails Quickstart

add this to your Gemfile

gem "friendly_id", "~> 4.0.0.beta14"

create new file migration

class CreateSubCategories < ActiveRecord::Migration
  def up
    add_column :users, :slug, :string
    add_index :users, :slug, :unique => true
  end
end

and your model

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, :use => :slugged
end
Advertisements

About me
Interested about Ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: