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

后端 未结 5 680
离开以前
离开以前 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: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.

提交回复
热议问题