سؤال

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