I'm using the gem Ancestry, and trying to build my routes to show the hierarchy between parents and children.
Location.rb
def to_param
if self.ancestors?
get_location_slug(parent_id) + "/" + "#{slug}"
else
"#{slug}"
end
end
def get_location_slug(location_id)
location = Location.find(location_id)
"#{location.slug}"
end
This works 99% perfectly, and is showing my routes cleanly - but it is showing "%2F" instead of a "/" in my route with a parent:
localhost:3000/locations/location-1 (perfect)
localhost:3000/locations/location-1%2Flocation-2 (not quite perfect)
Routes.rb (sharing just in case)
match 'locations/:id' => 'locations#show', :as => :location, :via => :get
match 'locations/:parent_id/:id' => 'locations#show', as: :location_child, via: :get
Bonus question: This currently covers root Locations and child Locations. How could I extend this to cover grandchild Locations and great grandchild Locations? Thanks in advance!