Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails FriendlyId and normalize_friendly_id

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
like image 979
jordan.baucke Avatar asked May 27 '12 19:05

jordan.baucke


1 Answers

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

like image 180
Matenia Rossides Avatar answered Sep 27 '22 21:09

Matenia Rossides