具有祖先的模型的轨道路线



我有一个模型类别:

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

此 URL 的路由外观如何?这可能吗?

我建议你这样做问题:

/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

也许您可以让您的程序使用此解决方案。

相关内容

  • 没有找到相关文章

最新更新