我无法理解ruby-on-rails 3路由中命名空间和范围之间的区别.
有人可以解释一下吗?
namespace "admin" do
resources :posts, :comments
end
scope :module => "admin" do
resources :posts, :comments
end
Run Code Online (Sandbox Code Playgroud)
alt*_*ive 103
不同之处在于生成的路径.
路径是admin_posts_path
和admin_comments_path
命名空间的路径,而它们只是posts_path
和comments_path
范围.
通过将:name_prefix
选项传递给作用域,可以获得与命名空间相同的结果.
ynk*_*nkr 66
示例总是帮助我,所以这是一个例子:
namespace :blog do
resources :contexts
end
Run Code Online (Sandbox Code Playgroud)
会给我们以下路线:
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"}
Run Code Online (Sandbox Code Playgroud)
使用范围......
scope :module => 'blog' do
resources :contexts
end
Run Code Online (Sandbox Code Playgroud)
会给我们:
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"}
Run Code Online (Sandbox Code Playgroud)
以下是关于这个主题的一些很好的解读:http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing
mon*_*ike 53
从导轨指南
"该命名空间范围会自动添加:as
,以及:module
和:path
前缀."
所以
namespace "admin" do
resources :contexts
end
Run Code Online (Sandbox Code Playgroud)
是相同的
scope "/admin", as: "admin", module: "admin" do
resources :contexts
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31004 次 |
最近记录: |