difference between scope and namespace of ruby-on-rails 3 routing

后端 未结 5 681
离开以前
离开以前 2020-12-07 08:11

I can\'t understand what the difference is between a namespace and a scope in the routing of ruby-on-rails 3.

Could someone please explain?

namespace         


        
相关标签:
5条回答
  • 2020-12-07 08:40

    The difference lies in the paths generated.

    The paths are admin_posts_path and admin_comments_path for the namespace, while they are just posts_path and comments_path for the scope.

    You can get the same result as a namespace by passing the :name_prefix option to scope.

    0 讨论(0)
  • 2020-12-07 08:42

    Both scope and namespace are scoping a set of routes to the given default options.
    Except that there are no default options for scope, and for namespace :path, :as, :module, :shallow_path and :shallow_prefix options all default to the name of the namespace.

    Available options for both scope and namespace correspond to those of match.

    0 讨论(0)
  • 2020-12-07 08:48

    examples always help me, so here is an example:

    namespace :blog do
      resources :contexts
    end
    

    will give us the following routes:

        blog_contexts GET    /blog/contexts(.:format)          {:action=>"index", :controller=>"blog/contexts"}
                      POST   /blog/contexts(.:format)          {:action=>"create", :controller=>"blog/contexts"}
     new_blog_context GET    /blog/contexts/new(.:format)      {:action=>"new", :controller=>"blog/contexts"}
    edit_blog_context GET    /blog/contexts/:id/edit(.:format) {:action=>"edit", :controller=>"blog/contexts"}
         blog_context GET    /blog/contexts/:id(.:format)      {:action=>"show", :controller=>"blog/contexts"}
                      PUT    /blog/contexts/:id(.:format)      {:action=>"update", :controller=>"blog/contexts"}
                      DELETE /blog/contexts/:id(.:format)      {:action=>"destroy", :controller=>"blog/contexts"}
    

    Using scope...

    scope :module => 'blog' do
      resources :contexts
    end
    

    Will give us:

         contexts GET    /contexts(.:format)           {:action=>"index", :controller=>"blog/contexts"}
                  POST   /contexts(.:format)           {:action=>"create", :controller=>"blog/contexts"}
      new_context GET    /contexts/new(.:format)       {:action=>"new", :controller=>"blog/contexts"}
     edit_context GET    /contexts/:id/edit(.:format)  {:action=>"edit", :controller=>"blog/contexts"}
          context GET    /contexts/:id(.:format)       {:action=>"show", :controller=>"blog/contexts"}
                  PUT    /contexts/:id(.:format)       {:action=>"update", :controller=>"blog/contexts"}
                  DELETE /contexts/:id(.:format)       {:action=>"destroy", :controller=>"blog/contexts"}
    

    Here is some good reading on the subject: http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing

    0 讨论(0)
  • 2020-12-07 08:51

    from the rails guide

    "The namespace scope will automatically add :as as well as :module and :path prefixes."

    so

    namespace "admin" do
      resources :contexts
    end
    

    is the same as

    scope "/admin", as: "admin", module: "admin" do
      resources :contexts
    end
    
    0 讨论(0)
  • 2020-12-07 08:53

    scope is bit complex, but provides more options to fine-tune exactly what you want to do.

    scope supports three options: module, path and as. If you see scope with all it options, it will be exactly same as namespace.

    In other words, routes generated by

    namespace :admin do
      resources :posts
    end
    

    is same as

    scope module: 'admin', path: 'admin', as: 'admin' do
      resources :posts
    end
    

    In other words, we can say that there are no default options for scope as compared to namespace. namespace add all these options by default. Thus using scope, we can more fine tune the routes as required.

    If you take a deep look into scope and namespace default behaviour, you will find that scope by default supports only :path option, where as namespace supports three options module, path and as by default.

    For more info, please refer a doc namespace-and-routing.

    0 讨论(0)
提交回复
热议问题