Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top