Rails 4.0 -- friendly_id配置

     Rails 4.0 -- friendly_id配置

实现效果:http://127.0.0.1:3000/article/1  =>  http://127.0.0.1:3000/article/书名

(1)Rails 4.0的friendly_id的版本必须是5.0以上。在Gemfile中加入,

gem 'friendly_id','~> 5.0.0.beta'

(2)运行,

rails g migration add_slug_to_articles slug:string

rake db:migrate
(3)db/migrate/add_slug_to_articles.rb 
add_column :articles, :slug, :string

add_index :articles, :slug
(4)models/article.rb 
class Article < ActiveRecord::Base

  extend FriendlyId

  friendly_id :name, :use => :slugged



  def should_generate_new_friendly_id?

    new_record?

  end

end

相关链接:

http://railscasts.com/episodes/314-pretty-urls-with-friendlyid

http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

你可能感兴趣的:(Rails)