7

我正在尝试创建一条新路线,以便我可以利用 RoR 的路径变量功能,即new_game_path。就我而言,我想使用load_game_path

我为适当的控制器创建了一个动作,当前路由如下:

resources :games do
    get 'load', on: :collection
end

每次我使用load_games_path时,它都会使用正确的 URI,但似乎重定向到 GamesController 的显示操作并显示游戏的继承显示视图。

我检查了rake 路线,我看到我新创建的路线似乎是所需的路径 /games/load(文件路径:/views/games/load.html.erb)

load_games GET    /games/load(.:format)       games#load/

耙路线:

    welcome_index GET    /welcome/index(.:format)    welcome#index
      players GET    /players(.:format)          players#index
              POST   /players(.:format)          players#create
   new_player GET    /players/new(.:format)      players#new
  edit_player GET    /players/:id/edit(.:format) players#edit
       player GET    /players/:id(.:format)      players#show
              PUT    /players/:id(.:format)      players#update
              DELETE /players/:id(.:format)      players#destroy
        games GET    /games(.:format)            games#index
              POST   /games(.:format)            games#create
     new_game GET    /games/new(.:format)        games#new
    edit_game GET    /games/:id/edit(.:format)   games#edit
         game GET    /games/:id(.:format)        games#show
              PUT    /games/:id(.:format)        games#update
              DELETE /games/:id(.:format)        games#destroy
        users GET    /users(.:format)            users#index
              POST   /users(.:format)            users#create
     new_user GET    /users/new(.:format)        users#new
    edit_user GET    /users/:id/edit(.:format)   users#edit
         user GET    /users/:id(.:format)        users#show
              PUT    /users/:id(.:format)        users#update
              DELETE /users/:id(.:format)        users#destroy
                     /players/:name(.:format)    players#index
   load_games GET    /games/load(.:format)       games#load
              GET    /games(.:format)            games#index
              POST   /games(.:format)            games#create
              GET    /games/new(.:format)        games#new
              GET    /games/:id/edit(.:format)   games#edit
              GET    /games/:id(.:format)        games#show
              PUT    /games/:id(.:format)        games#update
              DELETE /games/:id(.:format)        games#destroy
         root        /                           welcome#index

路线.rb:

     get "welcome/index"

     resources :players, :games, :users

     match '/players/:name'  => 'players#index'

     # match 'games/load(.:format)', :controller => 'games', :action => 'load'

      resources :games do
        collection do
          get 'load'
        end
      end

root :to => 'welcome#index'

我知道加载是控制器的预定义操作。为了确保这不是问题,我尝试了一个任意的操作名称 - 产生相同的结果。

我也试过这个没有成功:

match 'games/load(.:format)', :controller => 'games', :action => 'load'
4

1 回答 1

15

尝试

resources :games do
  collection do
    get 'load'
  end
end

现在它将'games/load'解释为'games/:id',并将:id参数设置为'load',并将'games/:id'路由到GamesController#show。

编辑:并确保resources :games在路由文件的前面没有另一个调用,即使 :games 只是几个参数之一resources :players, :games,因为如果有的话,你以后将无法插入集合方法。

于 2013-07-26T01:25:22.807 回答