Last active
December 28, 2015 13:09
-
-
Save maxcal/7505499 to your computer and use it in GitHub Desktop.
Proposed addition to friendly_id guide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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