路由错误:当拆分控制器路由到命名空间路由时,未初始化的常量

时间:2013-05-15 13:47:52

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

我有以下这样的路线。如果我有这样的路线可以吗?

#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,一切都很好。 我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

我认为您不需要在admin下对此进行命名空间。您只需填写SubscribersController上的所有操作,并适当设置createupdate等权限。

答案 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