Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to generate slugs (human-readable IDs) in Rails

You know, like myblog.com/posts/donald-e-knuth.

Should I do this with the built in parameterize method?

What about a plugin? I could imagine a plugin being nice for handling duplicate slugs, etc. Here are some popular Github plugins -- does anyone have any experience with them?

  1. http://github.com/rsl/stringex/tree/master
  2. http://github.com/norman/friendly_id/tree/master

Basically it seems like slugs are a totally solved problem, and I don't to reinvent the wheel.

like image 673
Tom Lehman Avatar asked Aug 19 '09 19:08

Tom Lehman


People also ask

How do you make a slug in rails?

Generating a slug to use in the URL and store it as an attribute in the database. Replace the ID by the generated slug in the Rails routing Helpers by overriding the method to_param in the models. Handle the request with the slug parameter in Controllers.

What is a slug in Ruby?

Slugs are a colloquial term for the “SEO-friendly” identifier part of a URL. Seen a lot in applications like blogs or public sites, they turn a URL like: https://blog.example.com/posts/123. into one like this: https://blog.example.com/posts/clickbait-teaser-goes-here.


1 Answers

In Rails you can use #parameterize

For example:

> "Foo bar`s".parameterize  => "foo-bar-s" 
like image 196
grosser Avatar answered Oct 10 '22 16:10

grosser