Skip to content

Commit d75e0d9

Browse files
author
Tsvetomila Mihaylova
committed
Recommended usage of human-readable URLs and the FriendlyId gem.
1 parent dddcfc9 commit d75e0d9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,32 @@ the same purpose of the named scope and returns and
205205
`ActiveRecord::Relation` object.
206206
* Beware of the behavior of the `update_attribute` method. It doesn't
207207
run the model validations (unlike `update_attributes`) and could easily corrupt the model state.
208+
* Use user-friendly URLs. Show some descriptive attribute of the model in the URL rather than its `id`.
209+
There is more than one way to achieve this:
210+
* Override the `to_param` method of the model. This method is used by Rails for constructing an URL to the object.
211+
The default implementation returns the `id` of the record as a String. It could be overridden to include another
212+
human-readable attribute.
213+
214+
```Ruby
215+
class Person
216+
def to_param
217+
"#{id} #{name}".parameterize
218+
end
219+
end
220+
```
221+
In order to convert this to a URL-friendly value, `parameterize` should be called on the string. The `id` of the
222+
object needs to be at the beginning so that it could be found by the `find` method of ActiveRecord.
223+
224+
* Use the `friendly_id` gem. It allows creation of human-readable URLs by using some descriptive attribute of the model instead of its `id`.
225+
226+
```Ruby
227+
class Person
228+
extend FriendlyId
229+
friendly_id :name, use: :slugged
230+
end
231+
```
232+
233+
Check the [gem documentation](https://github.com/norman/friendly_id) for more information about its usage.
208234
209235
## Migrations
210236
@@ -477,6 +503,8 @@ compatible with Ruby 1.9. Generates great reports. Must have!
477503
* [active_admin](https://github.com/gregbell/active_admin) - With ActiveAdmin the creation of admin interface
478504
for your Rails app is child's play. You get a nice dashboard, CRUD
479505
UI and lots more. Very flexible and customizable.
506+
* [friendly_id](https://github.com/norman/friendly_id) - Allows creation of human-readable URLs by using some
507+
descriptive attribute of the model instead of its id.
480508
481509
This list is not exhaustive and other gems might be added to it along
482510
the road. All of the gems on the list are field tested, have active

0 commit comments

Comments
 (0)