instance method
scope
Ruby on Rails 3.1.12
Since v3.0.20Signature
scope(*args)
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 /rails/projects/2, rather than /accounts/rails/projects/2.
Options
Takes same options as Base#match and Resources#resources.
Examples
# route /posts (without the prefix /admin) to <tt>Admin::PostsController</tt> 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
Source
# File actionpack/lib/action_dispatch/routing/mapper.rb, line 609
def scope(*args)
options = args.extract_options!
options = options.dup
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 609
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Routing::Mapper::Scoping