scope :module => "admin" do
resources :posts, :comments
end
or
resources :posts, :module => "admin"
scope "/admin" do
resources :posts, :comments
end
resources :posts, :path => "/admin/posts"
Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:
resources :magazines do
resources :ads
end
Verb | Path | action | used for |
---|---|---|---|
GET | /magazines/1/ads | index | display a list of all ads for a specific magazine |
GET | /magazines/1/ads/new | new | return an HTML form for creating a new ad belonging to a specific magazine |
POST | /magazines/1/ads | create | create a new ad belonging to a specific magazine |
GET | /magazines/1/ads/1 | show | display a specific ad belonging to a specific magazine |
GET | /magazines/1/ads/1/edit | edit | return an HTML form for editing an ad belonging to a specific magazine |
PUT | /magazines/1/ads/1 | update | update a specific ad belonging to a specific magazine |
DELETE | /magazines/1/ads/1 | destroy | delete a specific ad belonging to a specific magazine |
resources :photos do
member do
get 'preview'
end
end
This will recognize /photos/1/preview with GET, and route to thepreview action of PhotosController. It will also create the preview_photo_url and preview_photo_path helpers.
resources :photos do
collection do
get 'search'
end
end
This will enable Rails to recognize paths such as /photos/search withGET, and route to the search action of PhotosController. It will also create thesearch_photos_url and search_photos_path route helpers.
You can specify a name for any route using the :as option.
match 'exit' => 'sessions#destroy', :as => :logout
You can use the :via option to constrain the request to one or more HTTP methods:
match 'photos/show' => 'photos#show', :via => [:get, :post]
match 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
resources :photos, :as => "images"
will recognize incoming paths beginning with
/photos and route the requests to
PhotosController, but use the value of the :as option to name the helpers.
Using scope, we can alter path names generated by resources:
scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
resources :categories, :path => "kategorien"
end
HTTP verb | Path | action |
---|---|---|
GET | /kategorien | index |
GET | /kategorien/neu | new |
POST | /kategorien | create |
GET | /kategorien/1 | show |
GET | /kategorien/:id/bearbeiten | edit |
PUT | /kategorien/1 | update |
DELETE | /kategorien/1 | destroy |
The :as option overrides the automatically-generated name for the resource in nested route helpers. For example,
resources :magazines do
resources :ads, :as => 'periodical_ads'
end
If you want a complete list of all of the available routes in your application, runrake routes command.