对于个人项目,我想在Rails 4中创建一个Restful Web服务。
因此,我使用rails-api
创建了我的第一个项目,并添加以下代码:
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:inconstantize' 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:incontroller' 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:incall' rack (1.5.2) lib/rack/conditionalget.rb:25:in
call&#39;机架(1.5.2) lib / rack / head.rb:11:incall' 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:incall' 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:incall' 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:incall' 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:incall' 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:incall' 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:intagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in
标记&#39;铁路(4.0.4) lib / rails / rack / logger.rb:20:incall' actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in
来电&#39;机架(1.5.2) lib / rack / runtime.rb:17:incall' activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in
call&#39;架 (1.5.2)lib / rack / lock.rb:17:incall' 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:incall' rack (1.5.2) lib/rack/content_length.rb:14:in
来电&#39;机架(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&#39; /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in {_ 1}}阻止了start_thread&#39;
我在Stackoverflow上发现了很多相同错误的帖子,但答案并没有帮助我解决问题。
谢谢!
答案 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