将GET和POST收集路由添加到资源路由

Rit*_*its 2 ruby rest ruby-on-rails http

情况

我有一个可以访问的资源FootballPlayer:

GET /clubs/id/football_players
Run Code Online (Sandbox Code Playgroud)

但是,我想要一种只访问所选足球运动员的方法,如下所示:

GET /clubs/id/football_players/selected
Run Code Online (Sandbox Code Playgroud)

我在routes.rb中使用了以下代码:

resources :clubs do
  resources :football_players do
    collection do
      get 'selected'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我访问URL时,它会触发selected操作FootballPlayers.

问题

我也希望能够用一组其他足球运动员替换选择.这样做的逻辑请求是:

POST /clubs/id/football_players/selected
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加post 'selected'到routes.rb,它会将请求重定向到相同的selected操作.

问题

如何让两条路线重定向到两个不同的动作?或者这不可能,我是否需要在动作中区分GET和POST?如果是这样,我该怎么做?

jde*_*eno 9

明确指定两种方法的操作:

resources :clubs do
  resources :football_players do
    collection do
      get  'selected', :action => 'list_selected'
      post 'selected', :action => 'change_selected'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)