Domanda

So far my routes.rb file looks something like this:

resources :games do
  resources :planets do
    member do
      get 'index' as: :play_game
    end
  end
end

which creates these (when I check rake routes)

play_game_game_planet GET    /games/:game_id/planets/:id/index(.:format) planets#index
         game_planets GET    /games/:game_id/planets(.:format)           planets#index
                      POST   /games/:game_id/planets(.:format)           planets#create
     new_game_planet  GET    /games/:game_id/planets/new(.:format)       planets#new
    edit_game_planet  GET    /games/:game_id/planets/:id/edit(.:format)  planets#edit
         game_planet  GET    /games/:game_id/planets/:id(.:format)       planets#show
                      PATCH  /games/:game_id/planets/:id(.:format)       planets#update
                      PUT    /games/:game_id/planets/:id(.:format)       planets#update
                      DELETE /games/:game_id/planets/:id(.:format)       planets#destroy

but the path I want is (similar to the second line)

play_game GET    /games/:game_id/planets(.:format)           planets#index
È stato utile?

Soluzione

You already have the game_planets /games/:game_id/planets(.:format) planets#index route defined above -- the named route for which is game_planets. So I assume what you're wanting is a different name for the named route?

If that's what you're after and you can't be advised otherwise (i.e. other Rails developers looking at this would wonder why the non-standard named route structure) then you can do this:

resources :games do
  resources :planets
end

get 'games/:game_id/planets', 'planets#index', as: 'play_game'

This essentially creates a duplicate route with a special named route. But I'd recommend against this because:

  1. Index actions should use a plural named route (so at least use as: 'play_games')
  2. It's not standard. The named route play_game would make me think there was a PlayController that had a nested Game controller and that this would be a show action requiring a play object passed into it.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top