Reputation: 23576
I'd like the have the following routes:
sports/1
calls the show
method for sport with id = 1.
sports/about/1
shows an "about" page for sport with id = 1.
Right now I have:
resources :sports
in routes.rb
.
If I try to go to
sports/about/1
I get an error because of course no sport has id = about.
Can I do something like this?
To clarify, I just have an about
action in my sports_controller
. Thanks for both methods though (controller and action)
Upvotes: 1
Views: 5742
Reputation: 27
I think this is the most straight forward way to do it:
resources :sports do
collection do
resources 'about'
end
end
Or maybe:
namespace :sports do
resources 'about'
end
Depending on whether you want a separate about controller or not.
Upvotes: 0
Reputation: 792
You can do almost anything you want with rails routes.
EDIT: Now a more complete (and correct) solution:
First you declare a new REST action in the sports resource:
resources :sports do
get 'about', :on => :member
end
Then, to change the normal URL behavior url/sports/id/about to url/sports/about/id you should use:
match 'sports/about/:id', :controller => 'sports', :action => 'about'}
Upvotes: 4
Reputation: 1267
This should take care of your routes, but you really ought to check out the RailsGuides on the subject.
resources :sports do
resources :about
end
This will give you routes like sports/:sport_id/about/:id
. Of course you need to set the value of the sport in your form_for(@sport, @about) do |f|
call or something like it.
Upvotes: 1
Reputation: 2093
In the routes.rb
write the following in the same order
match 'sports/about/:id' => 'sports#show', :as => :sport
resources :sports
To show a sport use sport_path(@sport)
Upvotes: 0