Rails 4 API未初始化的常量API :: CoursesController

时间:2016-02-29 22:01:01

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

嘿所有我正在创建我的rails应用程序的Api与Android客户端一起使用但是当我设置api时我得到了这个错误:

"未初始化的常量API :: CoursesController"

应用程序/控制器/ API / courses_controller.rb

class API::CoursesController < ApplicationController::Base
  courses = Course.all
  render json: courses, status: 200
end

的routes.rb

Rails.application.routes.draw do
  resources :courses
  resources :categories
  resources :quizzes
  resources :quiz_questions
  resources :quiz_answers


  namespace :api do
    resources :courses
  end
end

配置/初始化/是inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

当我转到http://localhost:3000/api/courses链接时,我得到了这个:

uninitialized constant API::CoursesController

Rails.root: /Users/faisalchoura/Documents/uni/rocket/server

activesupport (4.2.3) lib/active_support/inflector/methods.rb:276:in `const_get'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:276:in `block in constantize'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `each'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `inject'
activesupport (4.2.3) lib/active_support/inflector/methods.rb:259:in `constantize'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:821:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.3) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.3) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.3) lib/active_support/callbacks.rb:84:in `run_callbacks'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/Users/faisalchoura/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/faisalchoura/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/faisalchoura/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

你能帮助我吗?我已经被困在这一段时间了

谢谢!

2 个答案:

答案 0 :(得分:1)

命名空间应该用模块完成。

module API
  class CoursesController < ApplicationController::Base
  end
end

答案 1 :(得分:0)

刚刚意识到这是一个愚蠢的错误,完全忘记将代码放在索引方法中,只是把它留在了类中。

class API::CoursesController < ApplicationController
  def index
    courses = Course.all
    render json: courses, status: 200
  end
end

解决了问题