對於一個個人項目,我想這樣創造的Rails 4在軌道4,5創建API - 未初始化的常量阿比:: V1 :: UsersController
一個RESTful Web服務,我用rails-api
,並添加創建了我的第一個項目以下代碼:
的routes.rb在MyProject的>配置目錄
MyProject::Application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :users
end
end
end
users_controll http://localhost:3000/api/v1/users/show
我有這樣的:MyProject的>應用程序>控制器> 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
鐵軌服務器和去這個網址er.rb錯誤:
uninitialized constant Api::V1::UsersController
Rails.root: /Users/Jean/Development/MyProject
Application Trace | Framework Trace | Full Trace activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in
const_get' activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in
block in constantize' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:ineach' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in
inject' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:inconstantize' 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:incontroller' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:44:in
call' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:71:inblock in call' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in
each' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:incall' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in
call' rack (1.5.2) lib/rack/etag.rb:23:incall' rack (1.5.2) lib/rack/conditionalget.rb:25:in
call' rack (1.5.2) lib/rack/head.rb:11:incall' actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:in
call' activerecord (4.0.4) lib/active_record/query_cache.rb:36:incall' activerecord (4.0.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in
call' activerecord (4.0.4) lib/active_record/migration.rb:373:incall' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in
block in call' 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:incall' actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in
call' actionpack (4.0.4) lib/action_dispatch/middleware/remote_ip.rb:76:incall' actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in
call' actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:incall' railties (4.0.4) lib/rails/rack/logger.rb:38:in
call_app' railties (4.0.4) lib/rails/rack/logger.rb:20:inblock 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:intagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in
tagged' railties (4.0.4) lib/rails/rack/logger.rb:20:incall' actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in
call' rack (1.5.2) lib/rack/runtime.rb:17:incall' activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in
call' rack (1.5.2) lib/rack/lock.rb:17:incall' actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in
call' railties (4.0.4) lib/rails/engine.rb:511:incall' railties (4.0.4) lib/rails/application.rb:97:in
call' rack (1.5.2) lib/rack/lock.rb:17:incall' rack (1.5.2) lib/rack/content_length.rb:14:in
call' rack (1.5.2) lib/rack/handler/webrick.rb:60:inservice' /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:inrun' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in
block in start_thread'
我發現很多帖子#2使用相同的錯誤,但答案沒有幫助我解決我的問題。
謝謝!
謝謝你解決了我的問題!我最終刪除了'user'目錄並像你說的那樣聲明我的控制器:'class Api :: V1 :: UsersController'。但是關於這篇文章:http://www.amberbit.com/blog/2014/2/19/building-and-documenting-api-in-rails/博客使用'module API'創建'UsersController'類,模塊V1'而不是'class Api :: V1 :: UsersController'。爲什麼? –
看起來更新的答案。 –
對不起,也許我在第一條評論中還不夠清楚。什麼我要說的是,博主聲明同級車'模塊API 模塊V1 類UsersController