문제

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