2

The Situation

I have a resource FootballPlayer that is accessible at:

GET /clubs/id/football_players

However, I want a way to access only the football players that are selected, like this:

GET /clubs/id/football_players/selected

I got this working with the following code in routes.rb:

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

When I access the URL, it triggers the selected action of FootballPlayers.

The Problem

I also want to be able to replace the selection with a set of other football players. A logical request to do that would be:

POST /clubs/id/football_players/selected

However, if I add post 'selected' to routes.rb, it will redirect the request to the same selected action.

The Question

How can I make the two routes redirect to two different actions? Or isn't this possible, and do I need to distinguish between GET and POST in the action myself? If so, how do I do that?

1
  • can you tell me how do you try to redirect to the post 'selected' action? Sep 7, 2011 at 15:55

2 Answers 2

10

Explicitly specify the actions for both methods:

resources :clubs do
  resources :football_players do
    collection do
      get  'selected', :action => 'list_selected'
      post 'selected', :action => 'change_selected'
    end
  end
end
2
  • But to what action would this redirect?
    – Rits
    Sep 7, 2011 at 16:18
  • @Rits, I've changed it to do what I think you want.
    – user324312
    Sep 7, 2011 at 16:27
0

[EDIT]

Haven't tried the answer above but I think the difference would be the generated route.. For this case, it would be clubs_football_players_selected_path and clubs_football_players_change_selected_path. On the other hand, both paths are clubs_football_players_selected_path (I'm just guessing).

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
  • 3
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – DimaSan
    Dec 16, 2016 at 11:42
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Flaggers / reviewers: For code-only answers such as this one, downvote, don't delete! Dec 16, 2016 at 23:19
  • Thanks, I've added an explanation.
    – jmjm
    Dec 22, 2016 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.