https://github.com/johno/urls_for_humans
Apply persistent, meaningful urls to your Rails app.
https://github.com/johno/urls_for_humans
Last synced: 9 months ago
JSON representation
Apply persistent, meaningful urls to your Rails app.
- Host: GitHub
- URL: https://github.com/johno/urls_for_humans
- Owner: johno
- License: mit
- Created: 2014-04-14T22:50:48.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2015-01-27T07:15:16.000Z (over 11 years ago)
- Last Synced: 2025-10-12T16:04:32.118Z (9 months ago)
- Language: Ruby
- Homepage: http://johnotander.com/rails/2014/04/24/urls-for-humans/
- Size: 223 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Urls for Humans
[](https://travis-ci.org/johnotander/urls_for_humans)
Urls for Humans is a gem that allows you to apply meaningful names to your Rails Application's urls
by leveraging what happens under the covers with `Model.find(params[:id])`, `to_i`, and `to_param`.
This makes it easy to turn `users/1` to `users/1-john-otander`. So long as the url is prefixed with
the model's `id` (which Urls for Humans ensures), the lookup will happen exactly how we intend it
to with a few key benefits:
* Simple thanks to ActiveSupport.
* Lightweight, weighing in at roughly 20 something lines of added gem code to your Rails app (since
ActiveSupport is already a dependency).
* Persistent urls because changes in the latter portions of a param won't affect it's lookup.
* There are no slugs required.
### Why use Urls for Humans in place of Friendly ID?
This is a different approach to friendly URLs than the `friendly_id` gem because it doesn't modify
the db queries themselves. The `urls_for_humans` approach essentially allows all urls fitting the
form `resource/-` to route to `resource/:id` because `to_i` is called on the
`id` parameter.
Personally, I prefer this approach because a link out there in the wild to a user's profile
`users/previous_username` isn't broken (404'd) when they change their username to `users/new_username`
because the slug has been changed.
Also, I'm biased.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'urls_for_humans'
```
And then execute:
```bash
$ bundle
```
Or install it yourself as:
```bash
$ gem install urls_for_humans
```
## Usage
To use Urls For Humans you need to extend the `UrlsForHumans` module, and call the class method
`urls_for_humans`:
```ruby
class User < ActiveRecord::Base
include UrlsForHumans
# ...
urls_for_humans :first_name, :last_name
# ...
end
```
The `urls_for_humans` method can be a collection of any information that you'd like to include in
the url. For example, with the above class we'd result in:
```ruby
u = User.create(first_name: 'John', last_name: 'Otander')
u.to_param
# => '1-john-otander'
u.first_name = nil
u.to_param
# => '1-otander'
```
With this solution, an ActiveRecord object will always produce the correct url throughout the
application:
```ruby
link_to user.first_name, user
# =>
*
## Contributing
1. Fork it ( http://github.com/johnotander/urls_for_humans/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
Crafted with <3 by [John Otander](http://johnotander.com) ([@4lpine](https://twitter.com/4lpine)).