问题
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