简体   繁体   中英

Use FriendlyId generator in model

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM