how to Set Title in Url - Ruby on Rails Friendly_id Gem

If You Guys looking any gem to set title in Url and don't want any ID, so I'll suggest friend_id gem, The friendly_id plugin can help by making it easy to generate a URL slug and maintain a history.
which is easy and totally amazing gem.

First thing First, Set Friendly_id gem in Gemfile
gem 'friendly_id'
Now,
rails g migration add_slug_to_articles slug:string
rake db:migrate
rails c
      >> Article.find_each(&:save)
rails g friendly_id

db/migrate/add_slug_to_articles.rb
add_column :articles, :slug, :string
add_index :articles, :slug

models/article.rb
class Article < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  def should_generate_new_friendly_id?
    new_record?
  end
end

# Or...

class Article < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :history
end

Tags: Ruby on Rails Tutorials, Ruby on Rails Videos, Ruby Gems, Gems Use trick, Ruby code, Rails servers

Comments