you are here: codestackercodes [RSS] → tag seo [RSS]

to_param

http://apidock.com/rails/Object/to_paramhttp://apidock.com/rails/Object/to_param#47-Better-slug-generation-essentially-a-to-param-replacement-

# create the slugify method

  class String
    def slugify
      returning self.downcase.gsub(/'/, '').gsub(/[^a-z0-9]+/, '-') do |slug|
        slug.chop! if slug.last == '-'
      end
    end
  end


#####################

# in model

  def to_param
    "#{id} #{title}".slugify
  end
Displaying 1 Code