i have this structure for my new urls:
module ApplicationHelper
def product_path(product)
unless product.category.nil?
cat = product.category
if cat.ancestry_depth.eql?(2)
product_long_path cat.root, cat.parent, cat, product
else
product_short_path cat.parent, cat, product
end
end
end
end
routes.rb file
get "/(*root_id)_(*subcategory_id)_(*category_id)_(*id)" => "products#show", :as => :product_long
get "/(*root_id)_(*category_id)_(*id)" => "products#show", :as => :product_short
and my show action
def show
@product = Product.find_by_slug params[:id]
cat = @product.category
raise NotFound unless cat.slug.eql?(params[:category_id]) and cat.root.slug.eql?(params[:root_id])
raise NotFound if cat.ancestry_depth.eql?(2) and !cat.parent.slug.eql?(params[:subcategory_id])
end
How can i build up a redirect for "products/:id", i have products with 2 and 3 category depth.
I’m do it the most obvious way:
ProductsController, show action
in Product model
But im not sure that this is most elegant solution, so im open for another solutions.