FriendlyID始终显示/slug和/1的重复内容。换句话说,为友好的插件(//slug-york)加载了正确的页面,但为旧的、不友好的插件(/11)加载了相同的内容。
这是我当前的配置:
#config/routes.rb
resources :groups, path: ''
get 'groups/:id' => redirect("/%{id}")
#app/models/group.rb
class Group < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
end
#app/controllers/groups_controller.rb
def show
@group = Group.friendly.find(params[:id])
end
作为一种潜在的解决方法,我发现把这个放到我的控制器中确实会将坏的插件(/11)重定向到好的插件(/new-york),但它感觉是错误的,原因有很多(路由到routes.rb之外,可能出现意想不到的后果,常见问题的复杂解决方案=可能不正确)。
if request.path != group_path(@group)
return redirect_to @group, :status => :moved_permanently
end
让FriendlyID (1)重定向:id调用到:slug或者(2)简单地404它们,正确的方法是什么?
发布于 2019-01-07 21:09:48
多亏了this fantastic comment on Medium,我现在有了一个功能齐全且非常优雅的解决方案,它解决了我最初的问题(使用/new-york和/11复制页面),并允许两个根级别的slug结构共存。
get '/:id', to: 'groups#show', constraints: proc {|req| FriendlyId::Slug.where(sluggable_type: 'Group').pluck(:slug).include?(req.params[:id])}, as: :group
get '/:id', to: 'custom_pages#show', constraints: proc {|req| FriendlyId::Slug.where(sluggable_type: 'CustomPage').pluck(:slug).include?(req.params[:id])}, as: :custom_page
https://stackoverflow.com/questions/54073143
复制相似问题