我有以下这样的路线。如果我有这样的路线可以吗?
#config/routes
resources :subscribers, only: [:index, :show]
namespace :admin do
resources :subscribers, only: [:new, :edit, :update, :create, :destroy]
end
我试图运行 rake routes ,结果是
admin_subscribers POST /admin/subscribers(.:format) admin/subscribers#create
new_admin_subscriber GET /admin/subscribers/new(.:format) admin/subscribers#new
edit_admin_subscriber GET /admin/subscribers/:id/edit(.:format) admin/subscribers#edit
admin_subscriber PUT /admin/subscribers/:id(.:format) admin/subscribers#update
DELETE /admin/subscribers/:id(.:format) admin/subscribers#destroy
subscribers GET /subscribers(.:format) subscribers#index
subscriber GET /subscribers/:id(.:format) subscribers#show
结果与我的期望相符,但是当我运行我的RSpec时,我遇到了错误
Routing Error
uninitialized constant Admin::SubscribersController
Try running rake routes for more information on available routes.
我在Rspec Helper中也添加了以下代码
#spec/spec_helper.rb
Spork.each_run do
###
if /spork/i =~ $0 || RSpec.configuration.drb?
ActiveSupport::Dependencies.clear
end
###
end
但是,如果我将浏览器激活到htt **:// l * alhost:3000 / admin / subscribers / new,一切都很好。 我错过了什么吗?
答案 0 :(得分:0)
我认为您不需要在admin
下对此进行命名空间。您只需填写SubscribersController
上的所有操作,并适当设置create
,update
等权限。
答案 1 :(得分:0)
如docs
中所述如果您想将/admin/subscribers
路由到SubscribersController
(不带Admin::
模块前缀),可以使用scope
代替namespace
#config/routes
resources :subscribers, only: [:index, :show]
scope "/admin" do
resources :subscribers, only: [:new, :edit, :update, :create, :destroy]
end