我正在使用friendly_id 5.0.0.rc1,还有active_admin.
看起来一切都按预期完美地工作,除了在active_admin中更新记录的slug属性/列没有做任何事情(它保持相同)
我发现只使用控制台的行为相同:
@H_404_6@p = Post.first p.slug #=> 'test' p.slug = 'another-test' p.save #=> true p.slug #=> 'test我的配置:
@H_404_6@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我的模特:
@H_404_6@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我的控制器:
@H_404_6@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谢谢!