Ruby-on-rails 3 라우팅의 범위와 네임 스페이스의 차이
ruby-on-rails 3의 라우팅에서 네임 스페이스와 범위의 차이점을 이해할 수 없습니다.
누군가 설명해 주시겠습니까?
namespace "admin" do
resources :posts, :comments
end
scope :module => "admin" do
resources :posts, :comments
end
차이점은 생성 된 경로에 있습니다.
경로는 다음 admin_posts_path
과 admin_comments_path
그들은 단지 반면, 네임 스페이스에 대한 posts_path
과 comments_path
범위에 대해.
:name_prefix
범위에 옵션을 전달하여 네임 스페이스와 동일한 결과를 얻을 수 있습니다 .
예제는 항상 도움이되므로 여기에 예제가 있습니다.
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
로부터 레일 가이드
"네임 스페이스 범위가 자동으로 추가됩니다 :as
뿐만 아니라 :module
및 :path
접두사."
그래서
namespace "admin" do
resources :contexts
end
와 같다
scope "/admin", as: "admin", module: "admin" do
resources :contexts
end
범위 와 네임 스페이스 모두 주어진 기본 옵션에 대한 경로 집합의 범위를 지정합니다.
거기에 대한 기본 옵션이없는 것을 제외 범위 등에 대한 네임 스페이스 :path
, :as
, :module
, :shallow_path
와 :shallow_prefix
네임 스페이스의 이름 옵션 모든 기본.
범위 및 네임 스페이스 모두에 대해 사용 가능한 옵션 은 일치 항목에 해당합니다 .
범위 는 약간 복잡하지만 원하는 작업을 정확하게 미세 조정할 수있는 더 많은 옵션을 제공합니다.
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.
'developer tip' 카테고리의 다른 글
각도 서비스 function ()에 $ scope 삽입 (0) | 2020.08.09 |
---|---|
DynamoDB의 로컬 인덱스와 글로벌 인덱스의 차이점 (0) | 2020.08.09 |
링커는 무엇을합니까? (0) | 2020.08.09 |
localhost에 대한 자체 서명 된 인증서를 만들려면 어떻게해야합니까? (0) | 2020.08.09 |
Windows 명령 프롬프트에서 .sh를 실행하는 방법은 무엇입니까? (0) | 2020.08.09 |