문제

Is there a way to access the FriendlyID slug generator from an instance of a model that extend FriendlyId?

And then pass it a string to have a unique slug generated?

도움이 되었습니까?

해결책

Below is one option for accessing the generator from the model. With this you can just include the :url_name or whatever you want to call it, in your form for making the object. In my case it is set up so if a string is not entered it sets the url_name to something else.

Class Foo  < ActiveRecord::Base

  has_friendly_id :url_name, :use_slug => true, :approximate_ascii => true

  attr_accessible :url_name

  def url_name
    read_attribute(:url_name) || "some other default"
  end

end

Also, this is a little older but for more information on FriendlyID checkout this railscast

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top