Search code examples
ruby-on-railsfriendly-id

Rails - friendly_id - How can I generate friendly_id slug from a text field


rails newbie here I am using friendly_id gem, I have a Page model when I create page a friendly_id slug generate from title given in text field, What i want is to able to edit that slug and do not change on update or when i change the title

here is my Page.rb

     class Page < ActiveRecord::Base

            belongs_to :user
            include Bootsy::Container

            extend FriendlyId

            friendly_id :title, use: :slugged


            validates :title, :presence => true
            validates :user_id, :presence => true

            def should_generate_new_friendly_id?

            end

          end

I know this is a very basic task but I am new in rails. thanks


Solution

  • Done this by adding the following login inside should_generate_new_friendly_id? :

    def should_generate_new_friendly_id?
      new_record? || !self.slug.present?
    end