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

2019-01-08 04:23发布

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.

12条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-08 05:02

In Rails you can use #parameterize

For example:

> "Foo bar`s".parameterize 
=> "foo-bar-s"
查看更多
倾城 Initia
3楼-- · 2019-01-08 05:03

I use the following, which will

  • translate & --> "and" and @ --> "at"
  • doesn't insert an underscore in place of an apostrophe, so "foo's" --> "foos"
  • doesn't include double-underscores
  • doesn't create slug that begins or ends with an underscore

  def to_slug
    #strip the string
    ret = self.strip

    #blow away apostrophes
    ret.gsub! /['`]/,""

    # @ --> at, and & --> and
    ret.gsub! /\s*@\s*/, " at "
    ret.gsub! /\s*&\s*/, " and "

    #replace all non alphanumeric, underscore or periods with underscore
     ret.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '_'  

     #convert double underscores to single
     ret.gsub! /_+/,"_"

     #strip off leading/trailing underscore
     ret.gsub! /\A[_\.]+|[_\.]+\z/,""

     ret
  end

so, for example:


>> s = "mom & dad @home!"
=> "mom & dad @home!"
>> s.to_slug
> "mom_and_dad_at_home"
查看更多
Explosion°爆炸
4楼-- · 2019-01-08 05:04

The main issue for my apps has been the apostrophes - rarely do you want the -s sitting out there on it's own.

class String

  def to_slug
    self.gsub(/['`]/, "").parameterize
  end

end
查看更多
聊天终结者
5楼-- · 2019-01-08 05:07

We use to_slug http://github.com/ludo/to_slug/tree/master. Does everything we need it to do (escaping 'funky characters'). Hope this helps.

EDIT: Seems to be breaking my link, sorry about that.

查看更多
贪生不怕死
6楼-- · 2019-01-08 05:08

I modified it a bit to create dashes instead of underscores, if anyone is interested:

def to_slug(param=self.slug)

    # strip the string
    ret = param.strip

    #blow away apostrophes
    ret.gsub! /['`]/, ""

    # @ --> at, and & --> and
    ret.gsub! /\s*@\s*/, " at "
    ret.gsub! /\s*&\s*/, " and "

    # replace all non alphanumeric, periods with dash
    ret.gsub! /\s*[^A-Za-z0-9\.]\s*/, '-'

    # replace underscore with dash
    ret.gsub! /[-_]{2,}/, '-'

    # convert double dashes to single
    ret.gsub! /-+/, "-"

    # strip off leading/trailing dash
    ret.gsub! /\A[-\.]+|[-\.]+\z/, ""

    ret
  end
查看更多
别忘想泡老子
7楼-- · 2019-01-08 05:10

Here is what I use:

class User < ActiveRecord::Base
  before_create :make_slug
  private

  def make_slug
    self.slug = self.name.downcase.gsub(/[^a-z1-9]+/, '-').chomp('-')
  end
end

Pretty self explanatory, although the only problem with this is if there is already the same one, it won't be name-01 or something like that.

Example:

".downcase.gsub(/[^a-z1-9]+/, '-').chomp('-')".downcase.gsub(/[^a-z1-9]+/, '-').chomp('-')

Outputs: -downcase-gsub-a-z1-9-chomp

查看更多
登录 后发表回答