我正在学习如何使用Rails。我按照这个网站上的教程进行操作:
http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails/
我将我的文件放在了app文件夹而不是lib文件夹中,并按照教程中所说编辑了application.rb文件。其余代码与教程中相同。
我完成了自定义错误格式化器,但如果我启动Rails服务器以测试所有内容,我会收到以下错误:
如果我没有启动服务器但调用
建议?
下面是编辑内容
应用程序.rb
我完成了自定义错误格式化器,但如果我启动Rails服务器以测试所有内容,我会收到以下错误:
Exiting
E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config/routes.rb:3:in `block in <top (required)>': uninitialized constant API (NameError)
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/routing/route_set.rb:337:in `instance_exec'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/routing/route_set.rb:337:in `eval_block'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/routing/route_set.rb:315:in `draw'
from E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config/routes.rb:1:in `<top (required)>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/routes_reloader.rb:40:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/routes_reloader.rb:16:in `reload!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/file_update_checker.rb:75:in `call'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/file_update_checker.rb:75:in `execute'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/routes_reloader.rb:27:in `updater'
from C:in `execute_if_updated'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application/finisher.rb:71:in `block in <module:Finisher>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/initializable.rb:30:in `instance_exec'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/initializable.rb:30:in `run'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/initializable.rb:55:in `block in run_initializers'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/initializable.rb:54:in `run_initializers'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/application.rb:300:in `initialize!'
from E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config/environment.rb:5:in `<top (required)>'
from E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config.ru:3:in `require'
from E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config.ru:3:in `block in <main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config.ru:in `new'
from E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config.ru:in `<main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/server.rb:50:in `app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/server.rb:130:in `log_to_stdout'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/server.rb:67:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:81:in `block in server'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:76:in `tap'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:76:in `server'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
如果我没有启动服务器但调用
rake routes
,我会收到以下错误信息:rake aborted!
NameError: uninitialized constant API
E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config/routes.rb:3:in `block in <top (required)>'
E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config/routes.rb:1:in `<top (required)>'
C:in `execute_if_updated'
E:/Users/Robert/Documents/Rubymine Projects/WarframeApi/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)
建议?
下面是编辑内容
应用程序.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module WarframeApi
class Application < Rails::Application
# Api load config
config.paths.add "app/api", glob: "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"]
end
end
Routes.rb
Rails.application.routes.draw do
mount API::Root => '/'
end
API文件位于./app/api目录中(如教程中建议的备选位置)。以下是完整的文件结构:
- app
- api
- root.rb
- v1
- authors.rb
- posts.rb
- root.rb
- api