Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby On Rails custom route always redirects to controller's show action

I am attempting to create a new route so that I can utilize RoR's path variable feature i.e. new_game_path. In my case, I want to use load_game_path

I have created an action for the appropriate controller and currently routed like so:

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

Every time I use the load_games_path it uses the correct URI but appears to redirect to the GamesController's show action and displays the Games' inherit show view.

I have checked rake routes and I see my newly created route with what seems to be the desired path /games/load (file path: /views/games/load.html.erb)

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'

I am aware that load is a pre-defined action for the controller. Just to make sure this wasn't an issue, I tried an arbitrary name for the action - yielding the same results.

I've also tried this with no success:

match 'games/load(.:format)', :controller => 'games', :action => 'load'
like image 579
Andrew Avatar asked Jul 26 '13 00:07

Andrew


1 Answers

Try

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

Right now it's interpreting 'games/load' as 'games/:id' with the :id parameter set to 'load', and routing 'games/:id' to GamesController#show.

Edit: And make sure there's not another call to resources :games earlier in the routes file, even if :games is just one of several arguments like resources :players, :games, because you won't be able to insert the collection method later if there is.

like image 111
bgates Avatar answered Nov 17 '22 04:11

bgates