所以,我使用rails-api创建了我的第一个项目,并添加了以下代码:
在MyProject中的routes.rb>配置目录
MyProject::Application.routes.draw do namespace :api,defaults: {format: 'json'} do namespace :v1 do resources :users end end end
MyProject中的users_controller.rb> app>控制器> api> v1>用户目录
module Api module V1 class UsersController < ApplicationController def index end def create end def show end def update end def delete end end end end
当我使用命令行rails s启动rails服务器并转到此URL:http:// localhost:3000 / api / v1 / users / show我遇到此错误:
uninitialized constant Api::V1::UsersController
Rails.root: /Users/Jean/Development/MyProject
Application Trace | Framework Trace | Full Trace activesupport (4.0.4)@H_403_20@ lib/active_support/inflector/methods.rb:228:in
const_get'@H_403_20@ activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in@H_403_20@
block in constantize’ activesupport (4.0.4)@H_403_20@ lib/active_support/inflector/methods.rb:224:ineach' activesupport@H_403_20@ (4.0.4) lib/active_support/inflector/methods.rb:224:in
inject’@H_403_20@ activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in@H_403_20@constantize' actionpack (4.0.4)@H_403_20@ lib/action_dispatch/routing/route_set.rb:76:in
controller_reference’@H_403_20@ actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:66:in@H_403_20@controller' actionpack (4.0.4)@H_403_20@ lib/action_dispatch/routing/route_set.rb:44:in
call’ actionpack@H_403_20@ (4.0.4) lib/action_dispatch/journey/router.rb:71:inblock in call'@H_403_20@ actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in
each’@H_403_20@ actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:incall'@H_403_20@ actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in@H_403_20@
call’ rack (1.5.2) lib/rack/etag.rb:23:incall' rack (1.5.2)@H_403_20@ lib/rack/conditionalget.rb:25:in
call’ rack (1.5.2)@H_403_20@ lib/rack/head.rb:11:incall' actionpack (4.0.4)@H_403_20@ lib/action_dispatch/middleware/params_parser.rb:27:in
call’@H_403_20@ activerecord (4.0.4) lib/active_record/query_cache.rb:36:incall'@H_403_20@ activerecord (4.0.4)@H_403_20@ lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in@H_403_20@
call’ activerecord (4.0.4) lib/active_record/migration.rb:373:in@H_403_20@call' actionpack (4.0.4)@H_403_20@ lib/action_dispatch/middleware/callbacks.rb:29:in
block in call’@H_403_20@ activesupport (4.0.4) lib/active_support/callbacks.rb:373:in@H_403_20@_run__4323212420903942114__call__callbacks' activesupport (4.0.4)@H_403_20@ lib/active_support/callbacks.rb:80:in
run_callbacks’ actionpack@H_403_20@ (4.0.4) lib/action_dispatch/middleware/callbacks.rb:27:incall'@H_403_20@ actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in@H_403_20@
call’ actionpack (4.0.4)@H_403_20@ lib/action_dispatch/middleware/remote_ip.rb:76:incall' actionpack@H_403_20@ (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in@H_403_20@
call’ actionpack (4.0.4)@H_403_20@ lib/action_dispatch/middleware/show_exceptions.rb:30:incall'@H_403_20@ railties (4.0.4) lib/rails/rack/logger.rb:38:in
call_app’ railties@H_403_20@ (4.0.4) lib/rails/rack/logger.rb:20:inblock in call' activesupport@H_403_20@ (4.0.4) lib/active_support/tagged_logging.rb:68:in
block in tagged’@H_403_20@ activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in@H_403_20@tagged' activesupport (4.0.4)@H_403_20@ lib/active_support/tagged_logging.rb:68:in
tagged’ railties (4.0.4)@H_403_20@ lib/rails/rack/logger.rb:20:incall' actionpack (4.0.4)@H_403_20@ lib/action_dispatch/middleware/request_id.rb:21:in
call’ rack (1.5.2)@H_403_20@ lib/rack/runtime.rb:17:incall' activesupport (4.0.4)@H_403_20@ lib/active_support/cache/strategy/local_cache.rb:83:in
call’ rack@H_403_20@ (1.5.2) lib/rack/lock.rb:17:incall' actionpack (4.0.4)@H_403_20@ lib/action_dispatch/middleware/static.rb:64:in
call’ railties (4.0.4)@H_403_20@ lib/rails/engine.rb:511:incall' railties (4.0.4)@H_403_20@ lib/rails/application.rb:97:in
call’ rack (1.5.2)@H_403_20@ lib/rack/lock.rb:17:incall' rack (1.5.2)@H_403_20@ lib/rack/content_length.rb:14:in
call’ rack (1.5.2)@H_403_20@ lib/rack/handler/webrick.rb:60:inservice'@H_403_20@ /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in@H_403_20@
service’@H_403_20@ /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in@H_403_20@run'@H_403_20@ /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in@H_403_20@
block in start_thread’
我发现Stackoverflow上有很多帖子有相同的错误,但答案并没有帮助我解决我的问题.
谢谢 !
解决方法
MyProject > app > controllers > api > v1 > users
控制器应该是类Api :: V1 :: Users :: UsersController@H_403_20@如果这:
MyProject > app > controllers > api > v1 > users_controller.rb
然后是类Api :: V1 :: UsersController
对于首字母缩写名称API :: V1 :: UsersController而不是Api :: V1 :: Users :: UsersController使用inflectors:
在config / initializers / inflections.rb中
ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'API' end