Module: ActionDispatch::Routing::Mapper::Resources
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | actionpack/lib/action_dispatch/routing/mapper.rb |
Overview
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
.
Constant Summary
-
CANONICAL_ACTIONS =
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1058%w(index create new show update destroy)
-
RESOURCE_OPTIONS =
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1057[:as, :controller, :path, :only, :except, :param, :concerns]
-
VALID_ON_OPTIONS =
CANONICAL_ACTIONS holds all actions that does not need a prefix or a path appended since they fit properly in their scope level.
[:new, :collection, :member]
Instance Attribute Summary
- #shallow readonly
- #shallow? ⇒ Boolean readonly
Instance Method Summary
-
#collection
To add a route to the collection:
-
#match(path, *rest)
match 'path' => 'controller#action' match 'path', to: 'controller#action' match 'path', 'otherpath', on:
:member
, via::get
. -
#member
To add a member route, add a member block into the resource block:
- #namespace(path, options = {})
- #nested
- #new
-
#resource(*resources, &block)
Sometimes, you have a resource that clients always look up without referencing an ID.
-
#resources(*resources, &block)
In Rails, a resourceful route provides a mapping between HTTP verbs and URLs and controller actions.
- #resources_path_names(options)
- #root(path, options = {})
- #using_match_shorthand?(path, options) ⇒ Boolean
Instance Attribute Details
#shallow (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1461
def shallow scope(:shallow => true) do yield end end
#shallow? ⇒ Boolean
(readonly)
[ GitHub ]
Instance Method Details
#collection
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1389
def collection unless resource_scope? raise ArgumentError, "can't use collection outside resource(s) scope" end with_scope_level(:collection) do scope(parent_resource.collection_scope) do yield end end end
#match(path, *rest)
match 'path' => 'controller#action' match 'path', to: 'controller#action' match 'path', 'otherpath', on: :member
, via: :get
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1474
def match(path, *rest) if rest.empty? && Hash === path = path path, to = .find { |name, _value| name.is_a?(String) } case to when Symbol [:action] = to when String if to =~ /#/ [:to] = to else [:controller] = to end else [:to] = to end .delete(path) paths = [path] else = rest.pop || {} paths = [path] + rest end [:anchor] = true unless .key?(:anchor) if [:on] && !VALID_ON_OPTIONS.include?( [:on]) raise ArgumentError, "Unknown scope #{on.inspect} given to :on" end if @scope[:controller] && @scope[:action] [:to] ||= "#{@scope[:controller]}##{@scope[:action]}" end paths.each do |_path| = .dup [:path] ||= _path if _path.is_a?(String) path_without_format = _path.to_s.sub(/\(\.:format\)$/, '') if using_match_shorthand?(path_without_format, ) [:to] ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1') [:to].tr!("-", "_") end decomposed_match(_path, ) end self end
#member
To add a member route, add a member block into the resource block:
resources :photos do
member do
get 'preview'
end
end
This will recognize /photos/1/preview
with GET, and route to the preview action of PhotosController
. It will also create the preview_photo_url
and preview_photo_path
helpers.
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1412
def member unless resource_scope? raise ArgumentError, "can't use member outside resource(s) scope" end with_scope_level(:member) do if shallow? shallow_scope(parent_resource.member_scope) { yield } else scope(parent_resource.member_scope) { yield } end end end
#namespace(path, options = {})
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1453
def namespace(path, = {}) if resource_scope? nested { super } else super end end
#nested
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1438
def nested unless resource_scope? raise ArgumentError, "can't use nested outside resource(s) scope" end with_scope_level(:nested) do if shallow? && shallow_nesting_depth >= 1 shallow_scope(parent_resource.nested_scope, ) { yield } else scope(parent_resource.nested_scope, ) { yield } end end end
#new
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1426
def new unless resource_scope? raise ArgumentError, "can't use new outside resource(s) scope" end with_scope_level(:new) do scope(parent_resource.new_scope(action_path(:new))) do yield end end end
#resource(*resources, &block)
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
creates six different routes in your application, all mapping to the Profiles
controller (note that the controller is named after the plural):
GET /profile/new
POST /profile
GET /profile
GET /profile/edit
PATCH/PUT /profile
DELETE /profile
Options
Takes same options as #resources.
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1192
def resource(*resources, &block) = resources. .dup if apply_common_behavior_for(:resource, resources, , &block) return self end resource_scope(:resource, SingletonResource.new(resources.pop, )) do yield if block_given? concerns( [:concerns]) if [:concerns] collection do post :create end if parent_resource.actions.include?(:create) new do get :new end if parent_resource.actions.include?(:new) set_member_mappings_for_resource end self end
#resources(*resources, &block)
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
PATCH/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
PATCH/PUT /photos/:photo_id/comments/:id
DELETE /photos/:photo_id/comments/:id
Options
Takes same options as Base#match as well as:
- :path_names
-
Allows you to change the segment component of the
edit
andnew
actions. Actions not specified are not changed.resources :posts, path_names: { new: "brand_new" }
The above example will now change /posts/new to /posts/brand_new
- :path
-
Allows you to change the path prefix for the resource.
resources :posts, path: 'postings'
The resource and all segments will now route to /postings instead of /posts
- :only
-
Only generate routes for the given actions.
resources :cows, only: :show resources :cows, only: [:show, :index]
- :except
-
Generate all routes except for the given actions.
resources :cows, except: :show resources :cows, except: [:show, :index]
- :shallow
-
Generates shallow routes for nested resource(s). When placed on a parent resource, generates shallow routes for all nested resources.
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
. - :shallow_path
-
Prefixes nested shallow routes with the specified path.
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 PATCH/PUT /sekret/comments/:id(.:format) comment DELETE /sekret/comments/:id(.:format)
- :shallow_prefix
-
Prefixes nested shallow route names with specified prefix.
scope shallow_prefix: "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_sekret_comment GET /comments/:id/edit(.:format) sekret_comment GET /comments/:id(.:format) sekret_comment PATCH/PUT /comments/:id(.:format) sekret_comment DELETE /comments/:id(.:format)
- :format
-
Allows you to specify the default value for optional
format
segment or disable it by supplyingfalse
.
Examples
# routes call {Admin::PostsController}
resources :posts, module: "admin"
# resource actions are at /admin/posts.
resources :posts, path: "admin/posts"
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1350
def resources(*resources, &block) = resources. .dup if apply_common_behavior_for(:resources, resources, , &block) return self end resource_scope(:resources, Resource.new(resources.pop, )) do yield if block_given? concerns( [:concerns]) if [:concerns] collection do get :index if parent_resource.actions.include?(:index) post :create if parent_resource.actions.include?(:create) end new do get :new end if parent_resource.actions.include?(:new) set_member_mappings_for_resource end self end
#resources_path_names(options)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1167
def resources_path_names( ) @scope[:path_names].merge!( ) end
#root(path, options = {})
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1566
def root(path, ={}) if path.is_a?(String) [:to] = path elsif path.is_a?(Hash) and .empty? = path else raise ArgumentError, "must be called with a path and/or options" end if @scope.resources? with_scope_level(:root) do scope(parent_resource.path) do super( ) end end else super( ) end end
#using_match_shorthand?(path, options) ⇒ Boolean
# File 'actionpack/lib/action_dispatch/routing/mapper.rb', line 1524
def using_match_shorthand?(path, ) path && ( [:to] || [:action]).nil? && path =~ %r{^/?[-\w]/[-\w/]$} end