Подтвердить что ты не робот

Разница между областью действия и пространством имен маршрутов ruby-on-rails 3

Я не могу понять, какая разница между пространством имен и областью в маршрутизации ruby-on-rails 3.

Может кто-нибудь объяснит?

namespace "admin" do
  resources :posts, :comments
end

scope :module => "admin" do
  resources :posts, :comments
end
4b9b3361

Ответ 1

Разница заключается в генерируемых путях.

Пути admin_posts_path и admin_comments_path для пространства имен, в то время как они просто posts_path и comments_path для области.

Вы можете получить тот же результат, что и пространство имен, передав параметр :name_prefix в область видимости.

Ответ 2

Примеры

всегда помогают мне, поэтому вот пример:

namespace :blog do
  resources :contexts
end

предоставит нам следующие маршруты:

    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"}

Использование области...

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

Дадим нам:

     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"}

Вот некоторые хорошие чтения по теме: http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing

Ответ 3

из руководства rails

"Область пространства имен автоматически добавит :as, а также :module и :path префиксы."

так

namespace "admin" do
  resources :contexts
end

совпадает с

scope "/admin", as: "admin", module: "admin" do
  resources :contexts
end

Ответ 4

Оба области и пространства имен просматривают набор маршрутов для заданных параметров по умолчанию.
За исключением того, что для области нет параметров по умолчанию, а для пространства имен :path, :as, :module, :shallow_path и :shallow_prefix все параметры по умолчанию относятся к имени пространства имен.

Доступные параметры для области и пространства имен соответствуют матч.