Rumel
Rumel

Reputation: 937

Map multiple paths to a controller

I have a controller called CardController. Currently I have routes like card_path that map to /cards/:id. I would like to make it so that I can use /trips/:id and /events/:id that map to the same /cards/:id. I know I'll have to override card_path eventually but is it possible to set up my routes file for this? Do I need to set up a Trip and Event controller that just redirect to the card actions?

Edit:

Trips should completely map to cards, meaning 'trips/1/edit' should end up at 'cards/1/edit', 'trips/1/images/12' should end up at 'cards/1/images/12'

Upvotes: 0

Views: 764

Answers (2)

Rumel
Rumel

Reputation: 937

I ended up adding some controller to the routes file.

routes.rb

def card_routes
  member do
    get 'test'
  end
end

class TripsController < CardsController; end

resources :trips { card_routes }
resources :cards { card_routes }

Now /trips/1/test and /cards/1/test go to the same place.

Upvotes: 1

TK-421
TK-421

Reputation: 10763

You can easily do something like:

get 'trips/:id' => 'cards#show'

Try accessing different trips in your browser, trips/1 or trips/2 (if cards with those ids exist), and they should redirect to the appropriate card.

If you haven't already, I recommend taking a few minutes and reading the Routing Guide, as it's really comprehensive and shows different ways of accomplishing things:

http://guides.rubyonrails.org/routing.html

Upvotes: 0

Related Questions