0

i'm editing a website (not made by me) and I don't undestand one thing.

Short intro When an user create a new product page, in product.rb, there is two validates

validates :title, presence: true
validates :slug, presence: true, uniqueness: true

The user can set only the title of the product and the slug (= the url) is generated automatically copying the title. For example, if I set title: "Red", the page's url will be mysite.com/products/red

Now the question/problem

If I create a page with the same title of another page, the title will be always "Red" (because there is no uniqueness, obviously) but the slug (generated automatically) will be something like:

mysite.com/products/red-af503270-0ffd-4654-b4e9-a210e6a6500e

A very long url.

Why? I can't find the script that is making it. Because I want to edit it to make the title progressive. For example mysite.com/products//red-2

J.Luca
  • 208
  • 1
  • 10

1 Answers1

0

this because the creator of web using friendly_id Gem, and the very long url is to make sure Uniqueness

When you try to insert a record that would generate a duplicate friendly id, FriendlyId will append a UUID to the generated slug to ensure uniqueness:

car = Car.create :title => "Peugeot 206"
car2 = Car.create :title => "Peugeot 206"

car.friendly_id #=> "peugeot-206"
car2.friendly_id #=> "peugeot-206-f9f3789a-daec-4156-af1d-fab81aa16ee5"

you can learn more to custom it from this link

widjajayd
  • 6,090
  • 4
  • 30
  • 41
  • oh, you have right. Just found `extend FriendlyId` and `friendly_id :title, use: [:slugged, :finders]` in the product.rb. Do you know how to make it shorter? – J.Luca May 11 '20 at 12:55
  • this answer has detail explanation if you want to change it , https://stackoverflow.com/a/25380607/938947 – widjajayd May 11 '20 at 13:08