2011-09-07 139 views
2

現狀添加GET和POST收集的路由資源的路由

我有一個資源FootballPlayer是可訪問的:

GET /clubs/id/football_players 

不過,我想辦法只能訪問足球球員被選中,是這樣的:

GET /clubs/id/football_players/selected 

我得到這個在routes.rb中下面的代碼工作:

resources :clubs do 
    resources :football_players do 
    collection do 
     get 'selected' 
    end 
    end 
end 

當我訪問URL時,它會觸發selected動作FootballPlayers

的問題

我也希望能夠取代選擇了一組其他足球運動員。一個合乎邏輯的要求做,這將是:

POST /clubs/id/football_players/selected 

但是,如果我添加post 'selected'到routes.rb中,它將請求重定向到同一selected行動。

問題

我怎樣才能讓兩個路由重定向到兩個不同的動作?或者這不可能,我是否需要在自己的操作中區分GET和POST?如果是這樣,我該怎麼做?

+0

你能告訴我你如何嘗試重定向到'選定'的行動? –

回答

8

明確指定了兩種方法的操作:

resources :clubs do 
    resources :football_players do 
    collection do 
     get 'selected', :action => 'list_selected' 
     post 'selected', :action => 'change_selected' 
    end 
    end 
end 
+0

但是,這將重定向到什麼動作? – Rits

+0

@Rits,我已經改變它做我想要的東西。 – jdeseno

+0

那很簡單。謝謝。 – Rits

0

[編輯]

沒有嘗試過上面的答案,但我認爲,差別是產生的途徑。對於這種情況,它將是clubs_football_players_selected_pathclubs_football_players_change_selected_path。另一方面,兩條路都是clubs_football_players_selected_path(我只是猜測)。

resources :clubs do 
    resources :football_players do 
    collection do 
     get :selected # or... get :list_selected, path: 'selected' 
     post :change_selected, path: 'selected' 
    end 
    end 
end 
+3

雖然此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan

+0

儘管此代碼片段可能會解決問題,但它並不能解釋爲什麼或如何回答問題。請[請提供您的代碼解釋](// meta.stackexchange.com/q/114762/269535),因爲這確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 **舉報人/評論者:** [僅用於代碼解答,如downvote,請勿刪除!](// meta.stackoverflow.com/a/260413/2747593) –

+0

謝謝,我添加了一個說明。 – jmmaniego