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?
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.