Rails friendly_id pages still attainable by record id

六眼飞鱼酱① 提交于 2019-12-23 18:53:31

问题


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.

Example:

I've a record with 'name' as slug. This record has 1 for ID.

So the page is attainable from 2 url's:

domain.com/name

and

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?


回答1:


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

What version of FriendlyId?

With 4.x, just use find_by_slug rather than find.

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.

Either way, you need to replace occurences of find with one of the alternatives above to tell your application to ignore the :id column.



来源:https://stackoverflow.com/questions/13331305/rails-friendly-id-pages-still-attainable-by-record-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!