2013-04-04 54 views
0

我要像一個路線:通過像傭工如何使用足智多謀的語義/語法來製作這些路線+助手?

get '/posts/new'   => 'posts#new' 
get '/:username/posts/:id' => 'posts#show' 
get '/:username/posts'  => 'posts#index' 

殘障:

new_post_path #=> new - 
post_path(post) #=> show - :username implied from post 
posts_path  #=> index - :username implied from session 
posts_path(user) #=> index - :username explicit 

我想和足智多謀的語義要做到這一點,而不是通過手工指定每個路線。此外,我不知道如何使url helpers變得聰明。

雖然?

回答

0

我假設你想要的是嵌套路線。這應該讓你更接近你正在尋找的東西。

在你的routes.rb文件

resources :users do 
    resoures :posts 
end 

這將使得這樣的路徑:

/users 
/users/:user_id 
/users/:user_id/posts 
/users/:user_id/posts/:id 

然後你就可以從那裏調整你的路線,這樣/posts/new點到您的文章控制器的東西像這樣:

(未經測試,並不確定這是否100%正確,所以有人請加入)

resources :users do 
    resoures :posts do 
    match "/posts/new", :to => "posts#new" 
    end 
end 
+0

我想要非典型的嵌套路線。 '/ users /:user_id/posts',而不是'/:username/posts'。這就是拋棄我。 – 2013-04-04 20:26:15

+0

對不起,我複製了錯誤的東西。查看更新的答案。我認爲這是'match'進來的地方。 – Catfish 2013-04-04 20:28:53