instance method
scope
Ruby on Rails 8.1.2
Since v3.0.20Signature
scope(*args, only: nil, except: nil, **options)
Scopes a set of routes to the given default options.
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 /:account_id/projects, rather than /accounts/:account_id/projects.
Options
Takes same options as Base#match and Resources#resources.
# route /posts (without the prefix /admin) to Admin::PostsController scope module: "admin" do resources :posts end # prefix the posts resource's requests with '/admin' scope path: "/admin" do resources :posts end # prefix the routing helper name: sekret_posts_path instead of posts_path scope as: "sekret" do resources :posts end
Parameters
-
argsrest -
onlykey = nil -
exceptkey = nil -
optionskeyrest
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 989
def scope(*args, only: nil, except: nil, **options)
if args.grep(Hash).any? && (deprecated_options = args.extract_options!)
only ||= assign_deprecated_option(deprecated_options, :only, :scope)
only ||= assign_deprecated_option(deprecated_options, :except, :scope)
assign_deprecated_options(deprecated_options, options, :scope)
end
scope = {}
options[:path] = args.flatten.join("/") if args.any?
options[:constraints] ||= {}
unless nested_scope?
options[:shallow_path] ||= options[:path] if options.key?(:path)
options[:shallow_prefix] ||= options[:as] if options.key?(:as)
end
if options[:constraints].is_a?(Hash)
defaults = options[:constraints].select do |k, v|
URL_OPTIONS.include?(k) && (v.is_a?(String) || v.is_a?(Integer))
end
options[:defaults] = defaults.merge(options[:defaults] || {})
else
block, options[:constraints] = options[:constraints], {}
end
if only || except
scope[:action_options] = { only:, except: }
end
if options.key? :anchor
raise ArgumentError, "anchor is ignored unless passed to `match`"
end
@scope.options.each do |option|
if option == :blocks
value = block
elsif option == :options
value = options
else
value = options.delete(option) { POISON }
end
unless POISON == value
scope[option] = send("merge_#{option}_scope", @scope[option], value)
end
end
@scope = @scope.new scope
yield
self
ensure
@scope = @scope.parent
end
Defined in actionpack/lib/action_dispatch/routing/mapper.rb line 989
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Routing::Mapper::Scoping