0

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!

Edgars
  • 913
  • 1
  • 19
  • 53

1 Answers1

1

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.

Thomas Klemm
  • 10,678
  • 1
  • 51
  • 54
  • Does this will work if I tell You that categorization functionality I build using gem called Ancestry ? – Edgars Mar 18 '13 at 09:59
  • Well, obviously you need to adapt how the slug is built in the method, but friendly_id doesn't care where the elements for the slug come from. – Thomas Klemm Mar 18 '13 at 10:09
  • I am pretty new in rails so maybe some guys have already done that so I could use it as example ? :) – Edgars Mar 18 '13 at 10:33