instance method scope

Ruby on Rails 3.0.20

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

scope(*args)

Used to scope a set of routes to particular constraints.

Take the following route definition as an example:

scope :path => ":account_id", :as => "account" do
  resources :projects
end

This generates helpers such as account_projects_path, just like resources does. The difference here being that the routes generated are like /rails/projects/2, rather than /accounts/rails/projects/2.

Supported options

:module

If you want to route /posts (without the prefix /admin) to Admin::PostsController, you could use

scope :module => "admin" do
  resources :posts
end
:path

If you want to prefix the route, you could use

scope :path => "/admin" do
  resources :posts
end

This will prefix all of the posts resource’s requests with ‘/admin’

:as

Prefixes the routing helpers in this scope with the specified label.

scope :as => "sekret" do
  resources :posts
end

Helpers such as posts_path will now be sekret_posts_path

:shallow_path

Prefixes nested shallow routes with the specified path.

scope :shallow_path => “sekret” do

resources :posts do
  resources :comments, :shallow => true
end

The comments resource here will have the following routes generated for it:

post_comments    GET    /sekret/posts/:post_id/comments(.:format)
post_comments    POST   /sekret/posts/:post_id/comments(.:format)
new_post_comment GET    /sekret/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)

Parameters

args rest
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 516
        def scope(*args)
          options = args.extract_options!
          options = options.dup

          if name_prefix = options.delete(:name_prefix)
            options[:as] ||= name_prefix
            ActiveSupport::Deprecation.warn ":name_prefix was deprecated in the new router syntax. Use :as instead.", caller
          end

          options[:path] = args.first if args.first.is_a?(String)
          recover = {}

          options[:constraints] ||= {}
          unless options[:constraints].is_a?(Hash)
            block, options[:constraints] = options[:constraints], {}
          end

          scope_options.each do |option|
            if value = options.delete(option)
              recover[option] = @scope[option]
              @scope[option]  = send("merge_#{option}_scope", @scope[option], value)
            end
          end

          recover[:block] = @scope[:blocks]
          @scope[:blocks] = merge_blocks_scope(@scope[:blocks], block)

          recover[:options] = @scope[:options]
          @scope[:options]  = merge_options_scope(@scope[:options], options)

          yield
          self
        ensure
          scope_options.each do |option|
            @scope[option] = recover[option] if recover.has_key?(option)
          end

          @scope[:options] = recover[:options]
          @scope[:blocks]  = recover[:block]
        end

Defined in actionpack/lib/action_dispatch/routing/mapper.rb line 516 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionDispatch::Routing::Mapper::Scoping

Type at least 2 characters to search.

↑↓ navigate · open · esc close