FriendlyID is a great gem.
It shouldn't be hard to implement user defined page URL.
Create table pages
with user_id
and link
class User < ActiveRecord::Base
has_many :pages
class Page < ActiveRecord::Base
belongs_to :user
has_friendly_id :link # link is name of the column whose value will be replaced by slugged value
On the page#new
you add an input for the link
attribute.
Alternatively, you could set friendly_id on title or something else with :use_slug => true option. This way FriendlyID will take the title and modify it so it doesn't have and restricted characters. It will use it's own table to store slugs. Use cached_slug
to increase performanse.
Updated
To give users a choice whether they wan't to set a custom link, you could do this:
- Set friendly_id on the
link
field without slugs..
- Make a virtual attribute
permalink
so you could show it in your forms.
- In the before_filter, check whether the
permalink
is set.
- If it is, write it to the
link
field.
- If it's not, write title to the
link
field.
FriendlyID uses babosa
gem to generate slugs. If you decide to use it as well, this is how your filter could look like:
protected
def generate_link
#you might need to use .nil? instead
self.link = self.permalink.empty? ? make_slug(self.title) : make_slug(self.permalink)
end
def make_slug(value)
value.to_slug.normalize.to_s #you could as well use ph6py's way
end