2011-04-29 94 views
2

目前我使用我的帖子#index動作顯示的情況下,所有的職位或filter'em按類別根據其規定:Rails的REST風格的URL:所有帖子某些類別

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 

所以/職位將返回所有的職位/職位/ BY_CATEGORY/1將返回類別下所有帖子1

我不知道是否有這樣做更REST風格,也許得到的一種方式一些漂亮的url_paths。

我讀過指南(使用最新的2.3 Rails分支),但是對於這種情況,嵌套路由和集合都不適用。謝謝:)

+0

你真的需要「by_category」網址嗎?這是鐵軌3? – corroded 2011-04-29 04:38:19

回答

6
resources :posts 
resources :categories do |categories| 
    categories.resources :posts 
end 

您的網址,然後:

/posts - 所有帖子

/posts/:id - 某些發佈

/categories - 所有類別

/categories/:id - 某一類

/categories/:id/posts - 某個類別內的所有帖子。

+0

謝謝,我感到困惑,它必須是那種自然的「反向」方式;) – 2011-04-29 04:50:43