Question

My task is to be able to see the path of the current category or product in browsing bar.

At this moment I just can see current category like this

localhost:3000/categories/smalcinataji

but I want like this

localhost:3000/categories/atkritumu-parstrades-tehnika/smalcinataji

To create pretty urls I am using gem called FriendlyId from this example http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

Thanks!

Was it helpful?

Solution

FriendlyId can take a method to construct the slug.

class Person < ActiveRecord::Base
  friendly_id :category_and_subcategory

  def category_and_subcategory
    "#{my_category_method}/#{my_subcategory_method}"
  end
end

Note that there might be an issue with routing due to the additional slash, but there's certainly a fix for this, too, if nescessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top