簡體   English   中英

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

[英]Create API in Rails 4 - Uninitialized constant Api::V1::UsersController

對於個人項目,我想在Rails 4中創建Restful Web服務。

所以,我使用rails-api創建了我的第一個項目,並添加了以下代碼:

MyProject> config目錄中的routes.rb

MyProject::Application.routes.draw do
    namespace :api, defaults: {format: 'json'} do
        namespace :v1 do
            resources :users
        end
    end
end

users_controller.rbMyProject的>應用>控制器> 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 const_get' activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in塊'activesupport(4.0。 4)lib / active_support / inflector / methods.rb:224:在each' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in inject'activesupport(4.0.4)lib / active_support / inflector / methods.rb:224:在constantize' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:76:in controller_reference'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調用'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每個'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電話'機架(1.5.2)lib / rack / etag.rb:23:在call' rack (1.5.2) lib/rack/conditionalget.rb:25:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:in調用'rack(1.5.2)lib / rack / head.rb:11:在call' actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:in調用'activerecord(4.0.4)lib / active_record / query_cache.rb:36:在call' activerecord (4.0.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in調用'activerecord(4.0.4) )lib / active_record / migration.rb:373:在call' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in塊中調用'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'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調用'actionpack(4.0.4)lib / action_dispatch / middleware / remote_ip.rb:76:在call' actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in調用'actionpack(4.0.4)lib / action_dispatch / mi中 ddleware / show_exceptions.rb:30:在call' railties (4.0.4) lib/rails/rack/logger.rb:38:in call_app'railties(4.0.4)lib / rails / rack / logger.rb:20: in block in call' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in block in tagged'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標記為'railties(4.0.4)lib / rails / rack / logger.rb:20:在call' actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in呼叫'機架(1.5.2)lib / rack / runtime.rb:17:在call' activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in呼叫'機架( 1.5.2)lib / rack / lock.rb:17:在call' actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in調用'railties(4.0.4)lib / rails / engine中。 rb:511:在call' railties (4.0.4) lib/rails/application.rb:97:in呼叫'機架(1.5.2)lib / rack / lock.rb:17:在call' rack (1.5.2) lib/rack/content_length.rb:14:in調用'rack(1.5.2)lib / rack / handler / webrick.rb中: 60: service' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in service'/Users/Jean/.rvm/rubies/ ruby-2.1.2 / lib / ruby​​ / 2.1.0 / webrick / httpserver.rb:94:in run' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in /Jean/ run' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in /rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick run' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in塊'

我發現Stackoverflow上有很多帖子有相同的錯誤,但答案並沒有幫助我解決我的問題。

謝謝 !

如果你有這條路:

MyProject> app> controllers> api> v1> users

控制器應該是class Api::V1::Users::UsersController如果這樣:

MyProject> app> controllers> api> v1> 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM