2010-05-23 55 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 /' 和所有類別在根路徑'/'