在Rails 4中创建API - 未初始化的常量Api :: V1 :: UsersController

时间:2014-07-20 14:43:03

标签: ruby-on-rails web-services api ruby-on-rails-4 routes

对于个人项目,我想在Rails 4中创建一个Restful Web服务。

因此,我使用rails-api创建了我的第一个项目,并添加以下代码:

MyProject>中的

routes.rb config 目录

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我收到此错误:

  

未初始化的常量Api :: V1 :: UsersController

     

Rails.root:/ Users / Jean / Development / MyProject

     

应用程序跟踪|框架跟踪|完整跟踪activesupport(4.0.4)   lib / active_support / inflector / methods.rb:228:在const_get' activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in 块中进行constantize&#39; activesupport(4.0.4)   lib / active_support / inflector / methods.rb:224:在each' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in注入&#39;   activesupport(4.0.4)lib / active_support / inflector / methods.rb:224:in   constantize' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:76:in controller_reference&#39;   actionpack(4.0.4)lib / action_dispatch / routing / route_set.rb:66:in   controller' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:44:in呼叫&#39; ActionPack的   (4.0.4)lib / action_dispatch / journey / router.rb:71:block in call' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in每个&#39;   actionpack(4.0.4)lib / action_dispatch / journey / router.rb:59:call' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in 来电&#39; rack(1.5.2)lib / rack / etag.rb:23:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:in call&#39;机架(1.5.2)   lib / rack / head.rb:11:in call' actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:in来电&#39;   activerecord(4.0.4)lib / active_record / query_cache.rb:36:in call' activerecord (4.0.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in call&#39; activerecord(4.0.4)lib / active_record / migration.rb:373:in   call' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in阻止通话&#39;   activesupport(4.0.4)lib / active_support / callbacks.rb:373:in   _run__4323212420903942114__call__callbacks' activesupport (4.0.4) lib/active_support/callbacks.rb:80:in run_callbacks&#39; ActionPack的   (4.0.4)lib / action_dispatch / middleware / callbacks.rb:27:in call' actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in call&#39; actionpack(4.0.4)   lib / action_dispatch / middleware / remote_ip.rb:76:in call' actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in 来电&#39; actionpack(4.0.4)   lib / action_dispatch / middleware / show_exceptions.rb:30:in call' railties (4.0.4) lib/rails/rack/logger.rb:38:in call_app&#39; railties   (4.0.4)lib / rails / rack / logger.rb:20:在block in call' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in块中标记为&#39;   activesupport(4.0.4)lib / active_support / tagged_logging.rb:26:in   tagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in标记&#39;铁路(4.0.4)   lib / rails / rack / logger.rb:20:in call' actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in来电&#39;机架(1.5.2)   lib / rack / runtime.rb:17:in call' activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in call&#39;架   (1.5.2)lib / rack / lock.rb:17:in call' actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in call&#39;铁路(4.0.4)   lib / rails / engine.rb:511:call' railties (4.0.4) lib/rails/application.rb:97:in来电&#39;机架(1.5.2)   lib / rack / lock.rb:17:in call' rack (1.5.2) lib/rack/content_length.rb:14:in来电&#39;机架(1.5.2)   lib / rack / handler / webrick.rb:60:in service' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in service&#39;   /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in   {_ 1}}阻止了start_thread&#39;

我在Stackoverflow上发现了很多相同错误的帖子,但答案并没有帮助我解决问题。

谢谢!

1 个答案:

答案 0 :(得分:14)

如果你有这条路:

  

MyProject&gt; app&gt;控制器&gt; api> v1&gt;用户

控制器应为class Api::V1::Users::UsersController  如果这样:

  

MyProject&gt; app&gt;控制器&gt; api> v1&gt; users_controller.rb

然后class 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