具有祖先的模型的Rails路由

时间:2012-04-15 12:42:07

标签: ruby-on-rails routes ancestry

我有一个型号类别:

class Category < ActiveRecord::Base
  has_many :item_categories, :dependent => :destroy
  has_many :items, :through => :item_categories
  has_ancestry
end

和模型项目:

class Item < ActiveRecord::Base
  has_many :item_categories, :dependent => :destroy
  has_many :categories, :through => :item_categories, :uniq => true
end

我使用这个宝石 - https://github.com/stefankroes/ancestry

因此,网站上的网址可以是:

/category-slug/ - "categories#show"
/category-slug/subcategory-slug
/category-slug/subcategory-slug/subsubcategory-slug
/category-slug/item-slug - "items#show"
/category-slug/subcategory-slug/item-slug
/category-slug/subcategory-slug/subsubcategory-slug/item-slug

这个网址的路线怎么样?有可能吗?

1 个答案:

答案 0 :(得分:0)

我建议你这样做:

/category1/ - "categories#show"
/category1/category2
/category1/category2/category3
/category1/item - "items#show"
/category1/category2/item
/category1/category2/category3/item-slug

我会通过以下路线解决这个问题:

resources :categories do
  resources :categroies
  resources :items
end

这给出了:

category_categroie GET /categories/:category_id/categroies/:id(.:format) {:action=>"show", :controller=>"categroies"}
     category_item GET /categories/:category_id/items/:id(.:format)      {:action=>"show", :controller=>"items"}
          category GET /categories/:id(.:format)                         {:action=>"show", :controller=>"categories"}

这解决了六个问题:

/categories/1
/categories/1/categories/2
/categories/2/categories/3
/categories/1/item/4
/categories/2/item/5
/categories/3/item/6

也许你可以让你的程序使用这个解决方案。