Facing some strange problem with respect to Slug in Rails
Loading development environment (Rails 3.2.13)
2.1.2 :001 > Tutorial.last
Tutorial Load (0.7ms) SELECT "tutorials".* FROM "tutorials"
ORDER BY "tutorials"."id" DESC LIMIT 1
=> #<Tutorial id: 3, title: "Populating the Database’s with seeds.rb",
state: "Publish", content_introduction: "<p>Demo Data</p>\r\n",
slug: "populating-the-database-s-with-seeds-rb">
2.1.2 :002 > Tutorial.last.slug
Tutorial Load (0.6ms) SELECT "tutorials".* FROM "tutorials"
ORDER BY "tutorials"."id" DESC LIMIT 1
=> "populating-the-database’s-with-seeds.rb"
In database it show "-" by replacing special character but while accessing it gives as it.
Model
def slug
title.strip.downcase.gsub(/[:,'"%^&*+=<>.`~]/,"").gsub("’","").gsub(" ", " ").gsub(" ", "-")
end
def to_param
"#{slug}".parameterize
end
extend FriendlyId
friendly_id :title, use: [ :slugged, :history ]
So while accessing the page using slug it gives error. Please have a look and suggest something.
There is a difference between what you see the value of slug in
Tutorial.lastand what you get inTutorial.last.slugTutorial.lastfetches the last record from the table which is giving you theslugsaved in the database, butTutorial.last.slugis calling theslugmethod defined in your model,Tutorial.lastis an object and using that object you are calling theslugmethodSo comment out the above method and you will get the same results in both cases!
Looks like the
slugmethod which you have defined in your model is manipulating thetitlefield to get the slug value, but as far as I knowfriendly_idgem itself handles it, so just comment out the slug method. It will solve your problem. Hope this helps!