سؤال

I am using friendly_id as follows:

class Page < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => [:slugged, :history]
end

I would like to be able to set the slug (i.e. be able to set a custom URL) without changing the title and maintaining old slugs in history.

Is there a straightforward way to do this using friendly_id or will I need to interact with the history table?

Thanks!

هل كانت مفيدة؟

المحلول

I accomplished this by adding an instance attribute url_seed:

class Page < ActiveRecord::Base
  extend FriendlyId
  friendly_id :slug_for_url, :use => [:slugged, :history]

  attr_accessible :title, :url_seed
  attr_accessor :url_seed

  def slug_for_url
    self.url_seed.blank? ? self.title : self.url_seed
  end
end

Now, on save, if url_seed is set, friendly_id uses the custom URL text to set the URL. Works like a charm :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top