对于个人项目,我想在Rails 4中创建一个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_controller.rb在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服务器rails s
并转到以下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:in
const_get' activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in
常量化activeactive(4.0.4)lib / active_support / inflector / methods.rb:224:each' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in
注入activeactive(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'动作包(4.0.4)lib / action_dispatch / routing / route_set.rb:66:在controller' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:44:in
通话中的动作包(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
每个动作包(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
呼叫机架中(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。call' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in
在通话的activesupport(4.0.4)中阻止lib / active_support / callbacks.rb:373:在_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:在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
调用中的动作包(4.0.4)lib / action_dispatch / middleware / show_exceptions.rb:30:在call' railties (4.0.4) lib/rails/rack/logger.rb:38:in
call_app的Railties中(4.0.4)lib / rails /机架/记录器。 rb:20:inblock in call' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in
标记为'activesupport(4.0.4)中的块lib / active_support / tagged_logging.rb:26:intagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in
标记为'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'机架(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
通话中的栏杆(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:incall' rack (1.5.2) lib/rack/content_length.rb:14:in
呼叫'机架(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
服务中'/Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver .rb:94:run' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in
在start_thread块中
我在Stackoverflow上发现了很多帖子,但都出现了相同的错误,但是答案并没有帮助我解决问题。
谢谢 !
如果您有以下路径:
MyProject>应用程序>控制器> api> v1>用户
控制器应该是class Api::V1::Users::UsersController
这样的:
MyProject>应用程序>控制器> api> v1> users_controller.rb
然后 class Api::V1::UsersController
对于首字母缩写词名称,请API::V1::UsersController
改用Api::V1::Users::UsersController
转折符:
在 config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句