Module | ActionDispatch::Routing::Mapper::Resources |
In: |
lib/action_dispatch/routing/mapper.rb
|
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code:
resources :photos
Sometimes, you have a resource that clients always look up without referencing an ID. A common example, /profile always shows the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action.
resource :profile
It‘s common to have resources that are logically children of other resources:
resources :magazines do resources :ads end
You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an admin namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:
namespace "admin" do resources :posts, :comments end
By default the +:id+ parameter doesn‘t accept dots. If you need to use dots as part of the +:id+ parameter add a constraint which overrides this restriction, e.g:
resources :articles, :id => /[^\/]+/
This allows any character other than a slash as part of your +:id+.
VALID_ON_OPTIONS | = | [:new, :collection, :member] | CANONICAL_ACTIONS holds all actions that does not need a prefix or a path appended since they fit properly in their scope level. | |
RESOURCE_OPTIONS | = | [:as, :controller, :path, :only, :except] | ||
CANONICAL_ACTIONS | = | %w(index create new show update destroy) |
To add a route to the collection:
resources :photos do collection do get 'search' end end
This will enable Rails to recognize paths such as /photos/search with GET, and route to the search action of PhotosController. It will also create the search_photos_url and search_photos_path route helpers.
Sometimes, you have a resource that clients always look up without referencing an ID. A common example, /profile always shows the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action:
resource :geocoder
creates six different routes in your application, all mapping to the GeoCoders controller (note that the controller is named after the plural):
GET /geocoder/new POST /geocoder GET /geocoder GET /geocoder/edit PUT /geocoder DELETE /geocoder
Takes same options as resources.
In Rails, a resourceful route provides a mapping between HTTP verbs and URLs and controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as
resources :photos
creates seven different routes in your application, all mapping to the Photos controller:
GET /photos GET /photos/new POST /photos GET /photos/:id GET /photos/:id/edit PUT /photos/:id DELETE /photos/:id
Resources can also be nested infinitely by using this block syntax:
resources :photos do resources :comments end
This generates the following comments routes:
GET /photos/:photo_id/comments GET /photos/:photo_id/comments/new POST /photos/:photo_id/comments GET /photos/:photo_id/comments/:id GET /photos/:photo_id/comments/:id/edit PUT /photos/:photo_id/comments/:id DELETE /photos/:photo_id/comments/:id
Takes same options as Base#match as well as:
resources :posts, :path_names => { :new => "brand_new" }
The above example will now change /posts/new to /posts/brand_new
resources :posts, :path => 'postings'
The resource and all segments will now route to /postings instead of /posts
resources :cows, :only => :show resources :cows, :only => [:show, :index]
resources :cows, :except => :show resources :cows, :except => [:show, :index]
resources :posts, :shallow => true do resources :comments end
Is the same as:
resources :posts do resources :comments, :except => [:show, :edit, :update, :destroy] end resources :comments, :only => [:show, :edit, :update, :destroy]
This allows URLs for resources that otherwise would be deeply nested such as a comment on a blog post like /posts/a-long-permalink/comments/1234 to be shortened to just /comments/1234.
scope :shallow_path => "sekret" do resources :posts do resources :comments, :shallow => true end end
The comments resource here will have the following routes generated for it:
post_comments GET /posts/:post_id/comments(.:format) post_comments POST /posts/:post_id/comments(.:format) new_post_comment GET /posts/:post_id/comments/new(.:format) edit_comment GET /sekret/comments/:id/edit(.:format) comment GET /sekret/comments/:id(.:format) comment PUT /sekret/comments/:id(.:format) comment DELETE /sekret/comments/:id(.:format)
# routes call <tt>Admin::PostsController</tt> resources :posts, :module => "admin" # resource actions are at /admin/posts. resources :posts, :path => "admin/posts"