Ruby On Rails自定义路由始终重定向到控制器的show动作

时间:2013-07-26 00:49:33

标签: ruby-on-rails ruby routing

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

我已为相应的控制器创建了一个操作,并且当前路由如此:

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

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

我已经检查了 rake路线,我看到我新创建的路线似乎是所需的路径/游戏/加载(文件路径:/views/games/load.html.erb)< / p>

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

Rake Routes:

    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

routes.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'

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

我也试过这个没有成功:

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

1 个答案:

答案 0 :(得分:12)

尝试

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

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

编辑:并确保路线文件中之前没有再次调用resources :games,即使:游戏只是resources :players, :games等几个参数中的一个,因为您将无法插入如果存在,则收集方法稍后。