我对生成带有title和id的slug很感兴趣。否则,我会收到像post收集路由覆盖单个post路由这样的错误。
class Post > ApplicationRecord
extend FriendlyId
friendly_id [:id, :title], use: :slugged
endresources :posts do
get "videos", on: :collection
endSlug "videos“将与"videos”收集路线冲突。
可以生成一个带有id和title的slug吗?
"#{id}_#{title}" # Friendly Id slug发布于 2021-01-21 15:25:51
友好Id不能这样做,因为它在保存之前生成slug,因此它不会有id https://github.com/norman/friendly_id/blob/master/lib/friendly_id/slugged.rb#L250
您可能需要使用to_param方法对其进行自定义:
class Post < ApplicationRecord
def to_param
"#{id}_#{title}".parameterize
end
end但是接下来你需要编写自定义方法来查找记录id,就像在你的控制器中一样:
def show
id = params[:id].split('_')[0]
post = Post.find(id)
endhttps://stackoverflow.com/questions/65817243
复制相似问题