Add Gem. (/Gemfile) 加 gem 到 Gemfile 中 gem 'friendly_id', '~> 5.1.0' gem 'babosa' 安裝套件 bundle install 建立Table ($rails c) rails g friendly_id rails g migration add_slug_to_posts slug:string:uniq rake db:migrate 修改Model (app/models/post.rb) class Post < ActiveRecord::Base belongs_to :user, foreign_key: :user_id has_many :comments has_many :post_categories has_many :categories, through: :post_categories has_many :votes, as: :voteable validates :title, presence: true, length: {minimum: 5} extend FriendlyId friendly_id :id, use: :slugged # 原本是input.to_s.parameterize,但是parameterize只支援 # 英文跟數字,所以改用babosa的to_slug def normalize_friendly_id(input) input.to_s.to_slug.normalize.to_s end def should_generate_new_friendly_id? slug.blank? || id_changed? # slug 為 nil 或 name column 變更時更新 end def slug_candidates [ :id, [:id, :title]] end end 使用friendly套件查找 (config/initializers/friendly_id.rb) config.us