0

I'm trying to modify my url parameter in my app. I'm currently using friendly_id to make the url include the title instead of the id. However I'd like to add the id after the title in the url if possible.

example:

current: app.com/categories/aircraft/listings/listing-title/

required: app.com/categories/aircraft/listings/listing-title/listing-id

Here's my nested routes right now

resources :categories do 
    resources :listings, only: [:index, :show]
end 

According to the documentation I can override the named helpers and change paths but I can only seem to override the listings part of the url and not actually just add an :id to the end of the parameter to keep it unique should there be two listings with the same title.

https://guides.rubyonrails.org/routing.html#overriding-the-named-helpers

I think friendly_id's addition of random unique characters to stop duplicate urls's is ugly so i'm thinking of a different way around it.

Adding the below route can kind of force it. But is there a way to add the route into the resources do block for show page only without effecting button links already on the page?

get 'categories/:catgeory_id/listings/:id/:aircraft_id', to: 'listings#show'    

EDITED:

I've used the advice of using slug_candidates

  def slug_candidates
    [
      :title,
      [:title, self.id]
    ]
  end

i've also tried it using just :id.

I'm receiving urls with the title/UUID instead of the title/listing_id by the looks of it. Any idea is this something to do with the migration having a unique field in the initial set up of the friendly_id gem?

Scott Bartell
  • 2,801
  • 3
  • 24
  • 36
Bradley
  • 922
  • 2
  • 8
  • 24

1 Answers1

0

Your solution kinda defeats the purpose of friendly-ids (not having to use ids in URLs). Try using candidates instead. Refer: http://norman.github.io/friendly_id/file.Guide.html#Candidates

def slug_candidates
  [
    :title,
    [:title, :id]
  ]
end

This will use title-id instead of title only if title happens to be repeated - not always like your solution.

tejasbubane
  • 914
  • 1
  • 8
  • 11
  • I kind of thought about SEO and wanted the SEO to have Category and Listing Title in the name but then after thinking about it. I knew as the listings are titles of vehicles that some would be the same title. So I thought by adding the id after the title like title/id (Not title-id) then even listings with the same title would just have a unique number after the title in the url keeping the url bit more readable than the random 20 odd characters friendly_id adds to a slug if its the same. – Bradley Mar 26 '19 at 19:28