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, updateand destroyactions, 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 adminnamespace. You would place these controllers under the app/controllers/admindirectory, and you can group them together in your router:

namespace "admin" do
  resources :posts, :comments
end

By default the :idparameter doesn't accept dots. If you need to use dots as part of the :idparameter 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.

Methods
C
M
N
R
S
U
W
Constants
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)
 
Instance Public methods
collection()

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/searchwith GET, and route to the search action of PhotosController. It will also create the search_photos_urland search_photos_pathroute helpers.

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1161
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)
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1249
def match(path, *rest)
  if rest.empty? && Hash === path
    options  = path
    path, to = options.find { |name, value| name.is_a?(String) }
    options[:to] = to
    options.delete(path)
    paths = [path]
  else
    options = rest.pop || {}
    paths = [path] + rest
  end

  if @scope[:controller] && @scope[:action]
    options[:to] ||= "#{@scope[:controller]}##{@scope[:action]}"
  end

  path_without_format = path.to_s.sub(/\(\.:format\)$/, '')
  if using_match_shorthand?(path_without_format, options)
    options[:to] ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1')
  end

  options[:anchor] = true unless options.key?(:anchor)

  if options[:on] && !VALID_ON_OPTIONS.include?(options[:on])
    raise ArgumentError, "Unknown scope #{on.inspect} given to :on"
  end

  paths.each { |_path| decomposed_match(_path, options.dup) }
  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/previewwith GET, and route to the preview action of PhotosController. It will also create the preview_photo_urland preview_photo_path helpers.

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1184
def member
  unless resource_scope?
    raise ArgumentError, "can't use member outside resource(s) scope"
  end

  with_scope_level(:member) do
    scope(parent_resource.member_scope) do
      yield
    end
  end
end
namespace(path, options = {})
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1231
def namespace(path, options = {})
  if resource_scope?
    nested { super }
  else
    super
  end
end
nested()
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1208
def nested
  unless resource_scope?
    raise ArgumentError, "can't use nested outside resource(s) scope"
  end

  with_scope_level(:nested) do
    if shallow?
      with_exclusive_scope do
        if @scope[:shallow_path].blank?
          scope(parent_resource.nested_scope, nested_options) { yield }
        else
          scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
            scope(parent_resource.nested_scope, nested_options) { yield }
          end
        end
      end
    else
      scope(parent_resource.nested_scope, nested_options) { yield }
    end
  end
end
new()
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1196
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 :geocoder

creates six different routes in your application, all mapping to the GeoCoderscontroller (note that the controller is named after the plural):

GET     /geocoder/new
POST    /geocoder
GET     /geocoder
GET     /geocoder/edit
PUT     /geocoder
DELETE  /geocoder

Options

Takes same options as resources.

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 981
def resource(*resources, &block)
  options = resources.extract_options!.dup

  if apply_common_behavior_for(:resource, resources, options, &block)
    return self
  end

  resource_scope(:resource, SingletonResource.new(resources.pop, options)) do
    yield if block_given?

    collection do
      post :create
    end if parent_resource.actions.include?(:create)

    new do
      get :new
    end if parent_resource.actions.include?(:new)

    member do
      get    :edit if parent_resource.actions.include?(:edit)
      get    :show if parent_resource.actions.include?(:show)
      put    :update if parent_resource.actions.include?(:update)
      delete :destroy if parent_resource.actions.include?(:destroy)
    end
  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 Photoscontroller:

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

Options

Takes same options as Base#matchas well as:

:path_names

Allows you to change the segment component of the editand newactions. 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/1234to 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 commentsresource 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)

Examples

# routes call <tt>Admin::PostsController</tt>
resources :posts, :module => "admin"

# resource actions are at /admin/posts.
resources :posts, :path => "admin/posts"
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1119
def resources(*resources, &block)
  options = resources.extract_options!.dup

  if apply_common_behavior_for(:resources, resources, options, &block)
    return self
  end

  resource_scope(:resources, Resource.new(resources.pop, options)) do
    yield if block_given?

    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)

    member do
      get    :edit if parent_resource.actions.include?(:edit)
      get    :show if parent_resource.actions.include?(:show)
      put    :update if parent_resource.actions.include?(:update)
      delete :destroy if parent_resource.actions.include?(:destroy)
    end
  end

  self
end
resources_path_names(options)
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 956
def resources_path_names(options)
  @scope[:path_names].merge!(options)
end
root(options={})
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1320
def root(options={})
  if @scope[:scope_level] == :resources
    with_scope_level(:root) do
      scope(parent_resource.path) do
        super(options)
      end
    end
  else
    super(options)
  end
end
shallow()
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1239
def shallow
  scope(:shallow => true, :shallow_path => @scope[:path]) do
    yield
  end
end
shallow?()
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1245
def shallow?
  parent_resource.instance_of?(Resource) && @scope[:shallow]
end
using_match_shorthand?(path, options)
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1280
def using_match_shorthand?(path, options)
  path && (options[:to] || options[:action]).nil? && path =~ %r{/[\w/]+$}
end
Instance Protected methods
with_exclusive_scope()
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1388
def with_exclusive_scope
  begin
    old_name_prefix, old_path = @scope[:as], @scope[:path]
    @scope[:as], @scope[:path] = nil, nil

    with_scope_level(:exclusive) do
      yield
    end
  ensure
    @scope[:as], @scope[:path] = old_name_prefix, old_path
  end
end
with_scope_level(kind, resource = parent_resource)
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 1401
def with_scope_level(kind, resource = parent_resource)
  old, @scope[:scope_level] = @scope[:scope_level], kind
  old_resource, @scope[:scope_level_resource] = @scope[:scope_level_resource], resource
  yield
ensure
  @scope[:scope_level] = old
  @scope[:scope_level_resource] = old_resource
end