Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

best way to limit slug length? #190

Closed
ghost opened this issue Nov 24, 2011 · 11 comments
Closed

best way to limit slug length? #190

ghost opened this issue Nov 24, 2011 · 11 comments

Comments

@ghost
Copy link

ghost commented Nov 24, 2011

I managed to save an item that has an epically long slug, but now I can't perform CRUD on it via a browser due to ...

Request-URI Too Large

WEBrick::HTTPStatus::RequestURITooLarge
WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09) at macbook.local:3000

What would be the best way to limit the slug's length. Why are they called slugs anyway, yuck.

@myabc
Copy link
Contributor

myabc commented Nov 24, 2011

Why are they called slugs anyway, yuck.

@bendwoodward http://en.wikipedia.org/wiki/Slug_(production)

@norman
Copy link
Owner

norman commented Nov 24, 2011

You can just use the regular validations offered by Active Record; i.e. validates_length_of, either on the slug itself, or if the value is coming from a column, then on the column.

@norman norman closed this as completed Nov 24, 2011
@ghost
Copy link
Author

ghost commented Nov 25, 2011

@myabc Interesting. I still couldn't find anything about the etymology of the word.

@norman Thanks.

@norman
Copy link
Owner

norman commented Nov 25, 2011

The term comes from the typesetting industry. IIRC Moveable Type was the blog engine that first used this term, since they used the typesetting metaphor throughout their software and materials.

@norman
Copy link
Owner

norman commented Nov 25, 2011

Oh. Just read @myabc's link. I am completely wrong.

@bhanuprasad143
Copy link

There is an option :max_length in friendly_id 3.x, which truncates slug to max_length. I cannot find anything similar in 4.0, but I think it is very useful option.

@norman
Copy link
Owner

norman commented Jan 30, 2012

@bhanuprasad143 ActiveSupport provides the limit method for this, so you could do something like:

my_string.parameterize.mb_chars.limit(140).to_s

You can also still use Babosa with FriendlyId if you want; that's the slug library extracted from FriendlyId 3, and has all of the string processing options that FriendlyId 3 had, though most of its functionality was incorporated into Rails 3.

@Sjors
Copy link

Sjors commented Jun 1, 2012

I just added this method to my model:

def normalize_friendly_id(string)
  super[0..139]
end

@lassebunk
Copy link

+1 to @Sjors' solution

@Dieglock
Copy link

  • another 1 to @Sjors. Was very handy...

@Empact
Copy link
Contributor

Empact commented Nov 4, 2015

@norman I think it was adopted by the newspaper industry based on its use in the typesetting industry: https://en.wikipedia.org/wiki/Slug_(typesetting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants