Rails RESTful URL:特定类别下的所有帖子 [英] Rails RESTful URL's: All Posts under certain Category
问题描述
当前,如果指定了以下类别,我将使用我的posts#index操作按类别显示所有帖子或filter'em:
Currently I use my posts#index action to show all posts or filter'em by category in case its specified:
PostsController:
def index
@posts = Post.all(:order => "created_at DESC")
@posts = @posts.by_category(params[:category_id]) #Custom named_scope
end
路线:
map.connect '/post/by_category/:category_id', :controller => :posts, :action => :index
map.resources :users
因此 /帖子将返回所有帖子,并且 / posts / by_category / 1 将返回第1类下的所有帖子
So /posts will return all the posts, and /posts/by_category/1 will return all posts under category 1
我想知道是否有一种方法可以使 RESTful 更加完善,甚至可以获得一些漂亮的url_paths。
I wonder if there is a way of doing it more RESTful, and maybe to get some pretty url_paths.
I已经阅读了指南(使用最新的2.3 Rails分支),但是嵌套路由和集合似乎都不适合这种情况。谢谢:)
I've read the guides (Using latest 2.3 Rails branch) but neither nested routes nor collections seemed appropiate for this case. Thanks :)
推荐答案
resources :posts
resources :categories do |categories|
categories.resources :posts
end
您的网址,然后:
/帖子
-所有帖子
/ posts /:id
-某些帖子
/类别
-所有类别
/ categories /:id
-某些类别
/ categories /:id / posts
-某个类别中的所有帖子。
/categories/:id/posts
- all posts within a certain category.
这篇关于Rails RESTful URL:特定类别下的所有帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!