Rails Friendly_id 页面仍然可以通过记录 ID 获得 [英] Rails friendly_id pages still attainable by record id
问题描述
我在我的 rails 应用程序中使用了friendly_id gem,以创建漂亮干净的 url.效果很好,但该页面也可以通过 te 记录 ID 访问.
I'am using the friendly_id gem in my rails app, to create nice clean url's. It's working out good, but the page are also attainable by te record id.
示例:
我有一个以名字"为蛞蝓的记录.此记录的 ID 为 1.
I've a record with 'name' as slug. This record has 1 for ID.
因此可以从 2 个 url 访问该页面:
So the page is attainable from 2 url's:
domain.com/name
domain.com/name
和
domain.com/1
domain.com/1
我只想使用 domain.com/name,所以我不想从 domain.com/1 访问该页面.
I want only to use domain.com/name, so I dont want the page to be attainable from domain.com/1.
有谁知道如何做到这一点?
Does anyone know how to accomplish this?
推荐答案
来自作者本人(Google 是你的朋友;这是搜索 friendly_id disable find id
时的最佳结果): https://github.com/norman/friendly_id/issues/146
From the author himself (Google is your friend; this is a top result when searching friendly_id disable find id
): https://github.com/norman/friendly_id/issues/146
FriendlyId 的版本是什么?
What version of FriendlyId?
对于 4.x,只需使用 find_by_slug 而不是 find.
With 4.x, just use find_by_slug rather than find.
在 3.x 中,您可以使用 find_by_cached_slug,或检查找到的结果的friendly_id_status 并在使用友好 ID 以外的其他内容找到记录时引发错误.
With 3.x you can use find_by_cached_slug, or check the friendly_id_status of the found result and raise an error if the record was found using something other than the friendly id.
无论哪种方式,您都需要将出现的 find
替换为上述替代方法之一,以告诉您的应用程序忽略 :id
列.
Either way, you need to replace occurences of find
with one of the alternatives above to tell your application to ignore the :id
column.
这篇关于Rails Friendly_id 页面仍然可以通过记录 ID 获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!