문제

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
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top