我正在使用friendly_id
5.0.0.rc1和active_admin
。friendly_id slu not不會更新
如預期,除了事實active_admin更新記錄的slug
屬性/列並不做任何事情這樣看來,一切都可以正常使用(它保持它相同)
我發現同樣的行爲只使用控制檯:
p = Post.first
p.slug
#=> 'test'
p.slug = 'another-test'
p.save
#=> true
p.slug
#=> 'test
我的配置:
FriendlyId.defaults do |config|
config.use :reserved
config.reserved_words = %w(admin new edit index session users register)
config.use :finders
config.use :slugged
config.slug_column = 'slug'
config.sequence_separator = '-'
config.use Module.new {
def should_generate_new_friendly_id?
slug.blank? || slug_changed?
end
}
end
我的模型:
class Post < ActiveRecord::Base
default_scope { order('created_at DESC') }
validates :title, presence: true
validates :body, presence: true
validates :views, presence: true, numericality: { only_integer: true }
extend FriendlyId
friendly_id :title, use: [:slugged, :history]
end
我的控制器:
class PostsController < ApplicationController
def index
@posts = Post.all.page(params[:page]).per(10)
end
def show
@post = Post.find_by_slug!(params[:id])
if request.path != post_path(@post)
redirect_to @post, :status => :moved_permanently and return
else
@post.increment :views if @post
end
end
end
謝謝!
也許我想錯了,但是您的配置狀態,當slug列改變了它應該產生一個新的團狀。所以如果你手動修改slug列的值,並保存記錄,我會假設生成過程被觸發,這會導致舊的slug,因爲標題沒有改變!? – Vapire
要回答你的問題:它不會觸發。默認情況下,他們解釋相反。所以你需要有你的def should_generate_new_friendly_id?在每個模型中。 – Ben