跳到主要內容

發表文章

目前顯示的是有「Gemfile」標籤的文章

Rails 網站不顯示id的方法

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...