views:

73

answers:

1

I have a Category model and a Product model.

Category has_many products 

and

Product belongs_to Category

I want my routes to be like this:

/:category_type/:category_name/  opens Product#index
/:category_type/  opens Category#index

/ opens Category#index

Is there a way to achieve that with resources? I tried with path_prefix but I just can't get it done.

Any help?

Thanks,

Nicolás Hock Isaza

A: 

Maybe this will help:

ActionController::Routing::Routes.draw do |map|

  map.category '/:category_type/', :controller => 'categories'
  map.category_products '/:category_type/:category_name/', :controller => 'products'
  map.root :controller => 'categories'

end

class CategoriesController < ApplicationController
  def index
    @categories = Category.find(:all) unless params[:category_type]
    @categories = Category.find_all_by_category_type if params[:category_type]
  end
end

class ProductsController < ApplicationController
  def index
    @category = Category.find_by_name(params[:category_name])
    @products = @category.products.find(:all)
  end
end

In this case you'll get categories filtred by type at '/:category_type/' and all categories at root path '/'

fantactuka