2010-05-23 53 views
0

我有一个类别模型和产品模型。Rails路线问题。总是找到名称和删除/ class_name /从路线

Category has_many products 

Product belongs_to Category 

我希望我的路线是这样的:

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

/opens Category#index 

有没有办法实现与资源?我尝试过使用path_prefix,但我无法完成。

任何帮助?

感谢,

尼古拉斯福伊萨萨

回答

0

也许这将帮助:

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 

在这种情况下,你会得到类别,根据类型在filtred '/:category_type /' 和所有类别在根路径'/'