6
votes

Trying to get my app running the FriendlyId gem (version 4.0.1)

I think I'm doing this in the wrong order, but I want to strip out apostrophes before my friendly_id slug is generated when creating a new record. But I think the method normalize_friend_id is being called after the id is already generated.

I've added the following to my model:

class Team < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, :use => :slugged

  def normalize_friendly_id(string)
    super.gsub("\'", "")
  end

end
1

1 Answers

12
votes

super calls the superclass first, meaning the friendly id is being generated then you're running gsub on THAT result. What you really want is to override this method completely.

Refer to: https://github.com/norman/friendly_id/blob/master/lib/friendly_id/slugged.rb#L244-246

your code should look like this:

def normalize_friendly_id(string)
  string.to_s.gsub("\'", "").parameterize
end

or

def normalize_friendly_id(string)
  super(string.to_s.gsub("\'", ""))
end

Hope that helps