Skip to content

Instantly share code, notes, and snippets.

@maxcal
Last active December 28, 2015 13:09
Show Gist options
  • Select an option

  • Save maxcal/7505499 to your computer and use it in GitHub Desktop.

Select an option

Save maxcal/7505499 to your computer and use it in GitHub Desktop.
Proposed addition to friendly_id guide
User editable slug - with defaults
----------------------------------
Case:
We want our post class to have slug and title attribues that are editable by the user.
The user should be able to edit the title and slug independently.
The slug should default to a slugged version of the title.
example output:
```ruby
post = Post.create(title: "How Long is a Long Long Time", slug: 'how-long')
post.slug
# 'how-long'
post = Post.create(title: "How Long is a Long Long Time")
post.slug
# 'how-long-is-a-long-long-time'
```
```ruby
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, :use => [:slugged]
validates_prescence_of :title
def should_generate_new_friendly_id?
if !slug?
name_changed?
else
false
end
end
end
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment