在 Rails 4 中,我有 3 个模型
class Tag < ActiveRecord::Base
# attr_accessible :id, :name
end
class Category < ActiveRecord::Base
# attr_accessible :id, :name
end
class Product < ActiveRecord::Base
# attr_accessible :id, :name
belongs_to :tag
belongs_to :category
delegate :tag_name, to: :tag
delegate :category_name, to: :category
end
现在我有一个标签id: 1, name: "tag1"
和一个类别id: 1, name: "category1"
,以及一个产品name: "product1", tag_id: 1, category_id: 1
我想将路由product
与URLtag
和category
URL 匹配。前任:
/tag1/category1/product1
/category1/tag1/product1
/tag1/product1
/category1/product1
/product1
但不知道如何自动添加它。(我使用friendly_id
gem 使 URL 变得更友好)这是我用来匹配路由的帖子,但它不是我想要的动态。当需求不仅tag
和category
, 而且sub_category
, super_category
...routes.rb
DRY
谁能给我另一个建议?