Rails的:我如何航线无关联的资源? [英] Rails: How do I route unassociated resources?
问题描述
我有四个型号:
class User < ActiveRecord::Base
has_many :posts
end
class Category < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :metric
has_many :comments
end
class Comments < ActiveRecord::Base
belongs_to :post
end
我希望能够在一定类别的访问后为特定用户。例如:
I'd like to be able to access a post within a certain category for a specific user. For example:
http://domain.com/users/1/categories/1/posts
此外,我希望看到所有类别的清单,如果我访问:
Also I want to see a list of all categories if I visit:
http://domain.com/users/1/categories/
和由于这些类别是固定同样为所有用户,用户和类别没有直接关联。正因为如此,我不太确定如何配置的routes.rb文件类别内访问职位的用户。我倒是AP preciate任何建议。谢谢!
Since categories are fixed and the same for all users, User and Category don't have a direct association. Because of this, I'm not quite sure how to configure the routes.rb file to access posts within a category for a user. I'd appreciate any suggestions. Thanks!
推荐答案
您可以使用嵌套的资源,要做到这一点,如:
You could use nested resources to do that like :
resources :users do
resources :categories
end
有关更多: http://guides.rubyonrails.org/routing.html#nested -resources
这篇关于Rails的:我如何航线无关联的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!