Rails4:将子目录中的控制器设置为路由文件中的根路径?

时间:2015-09-29 10:58:19

标签: ruby-on-rails ruby-on-rails-4 routes

我的controllers文件夹中有一个'api'子目录,其中有一个名为todo_list_controller的控制器。我一直在尝试将此控制器的索引设置为根路径,但我不断收到以下错误:

uninitialized constant TodoListController
下面的

是我的路线文件的一个例子:

Rails.application.routes.draw do
  namespace :api do
    resources :todo_lists
  end

  root 'todo_list#index'
end

完整错误堆栈跟踪:

uninitialized constant TodoListController

Rails.root: /home/ubuntu/workspace

Application Trace | Framework Trace | Full Trace

activesupport (4.1.0) lib/active_support/inflector/methods.rb:238:in `const_get'
activesupport (4.1.0) lib/active_support/inflector/methods.rb:238:in `block in constantize'
activesupport (4.1.0) lib/active_support/inflector/methods.rb:236:in `each'
activesupport (4.1.0) lib/active_support/inflector/methods.rb:236:in `inject'
activesupport (4.1.0) lib/active_support/inflector/methods.rb:236:in `constantize'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:76:in `controller_reference'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:66:in `controller'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:44:in `call'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:676:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.0) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.0) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
actionpack (4.1.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.0) lib/rails/engine.rb:514:in `call'
railties (4.1.0) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Routes

Routes match in priority from top to bottom

Helper  HTTP Verb   Path    Controller#Action
Path / Url      
Path Match
api_todo_lists_path GET /api/todo_lists(.:format)   api/todo_lists#index
POST    /api/todo_lists(.:format)   api/todo_lists#create
new_api_todo_list_path  GET /api/todo_lists/new(.:format)   api/todo_lists#new
edit_api_todo_list_path GET /api/todo_lists/:id/edit(.:format)  api/todo_lists#edit
api_todo_list_path  GET /api/todo_lists/:id(.:format)   api/todo_lists#show
PATCH   /api/todo_lists/:id(.:format)   api/todo_lists#update
PUT /api/todo_lists/:id(.:format)   api/todo_lists#update
DELETE  /api/todo_lists/:id(.:format)   api/todo_lists#destroy
root_path   GET /   todo_list#index
page_path   GET /pages/*id  high_voltage/pages#show

1 个答案:

答案 0 :(得分:2)

写作时

root "todo_list#index" 

rails将查找包含类app/controllers/todo_list_controller.rb的文件TodoListController。由于我假设控制器已经存在,在api/todo_lists_controller.rb中你应该写一些类似

的东西
root 'api/todo_lists#index'