命名空间控制器,无需命名空间路由助手

时间:2013-04-24 21:08:16

标签: ruby-on-rails routing

是否可以使用app/controllers/admin/categories_controller.rb代替categories_path来引用admin_categories_path

我正在使用Rails 4。

# app/controllers/admin
class Admin::CategoriesController < Admin::BaseController
end

# visiting localhost:3000/admin/categories causes route not found error 'admin/categories'
scope module: "admin" do
  resources :categories
end

# visiting localhost:3000/admin/categories causes uninitialized constant CategoriesController
scope "/admin" do
  resources :categories
end

1 个答案:

答案 0 :(得分:3)

我相信您必须在资源中引用控制器

scope 'admin' do
  resources :categories, controller: 'admin/categories'
end

所以路线成了

   categories   GET    /admin/categories(.:format)          admin/categories#index
                POST   /admin/categories(.:format)          admin/categories#create
   new_category GET    /admin/categories/new(.:format)      admin/categories#new
  edit_category GET    /admin/categories/:id/edit(.:format) admin/categories#edit
       category GET    /admin/categories/:id(.:format)      admin/categories#show
                PATCH  /admin/categories/:id(.:format)      admin/categories#update
                PUT    /admin/categories/:id(.:format)      admin/categories#update
                DELETE /admin/categories/:id(.:format)      admin/categories#destroy