- 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.use :finders # 把註解取消,啟動自動查找功能
- 已經有資料,想直接更新 slug,就直接在 rails console 中執行:
Post.find_each(&:save)
在網域內通常為了安全性會防堵所有對外不必要的端口,因此常會在網域內架設一Time Server做為各伺服器時間校對的標準,而此一Time Server需透過UDP 123端口與中央標準局time.stdtime.gov.tw做時間校對,再網域內的其他伺服器則與此Time Server做時間校對。 開啟防火牆NTP端口(UDP 123) 編輯系統登錄檔 [執行] -> regedit 修改NTP Server為中央標準局主機 time.stdtime.gov.tw 並將Type預設值NT5DS改為NTP [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters] "NtpServer"="ComputerName" "Type"="NTP" 修改校對頻率為a (每10分鐘校對一次,5為每5分鐘校對一次) [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config] "AnnounceFlags"=dword:0000000a 重啟Windows Time Service net stop w32time && net start w32time 強制立即校對 w32tm /resync /rediscover 補充一: 強制時間同步指令 w32tm /resync /computer:IP /rediscover net time \IP /set /yes net time \\ComputerName /set /yes 若您的主機一直無法同步,也可以採用變通的方法,配合上面的Dos command以工作排程來進行。 補充二: A. 將伺服器類型變更為 NTP。如果要執行這項操作,請依照下列步驟執行: 1. 按一下 [開始],再按一下 [執行],輸入 regedit,然後按一下 [確定]。 2. 找出並按一下下列登錄子機碼: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\Ty...
留言
張貼留言