Reputation: 1369
Currently I need to use a resource :id to load a page, but thought it would be a nice touch to display the friendly id :slug by default. I am already using norman/friendly-id
If i visit
app.example.com/houses/1
I want the address bar to show me
app.example.com/houses/128-prinsep-st-singapore-1
Does friendly-id already support this? Or what would be a nice way to do it?
Upvotes: 1
Views: 202
Reputation: 2472
FriendlyId provides a "slug candidates" functionality to let you specify alternate slugs to use in the event the one you want to use is already taken. For example:
class Example < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
# Try building a slug based on the following fields in
# increasing order of specificity.
def slug_candidates
[
:name,
[:name, :city],
[:name, :street, :city],
[:name, :street_number, :street, :city]
]
end
end
e1 = Example.create! name: 'Plaza Diner', city: 'New Paltz'
e2 = Example.create! name: 'Plaza Diner', city: 'Kingston'
e1.friendly_id #=> 'plaza-diner'
e2.friendly_id #=> 'plaza-diner-kingston'
Hope this will work for you.
Upvotes: 2