我正在开发一个包含使用API
gem的grape
的应用程序。
这是我的Root
类:
module API
class Root < Grape::API
rescue_from :all do |e|
Rack::Response.new(
[ "Error: #{e.message}" ],
500,
{ "Content-type" => "text/error" }
).finish
end
prefix "api"
version 'v1', using: :path
format :json
error_format :json
mount ::API::ServiceRequests
end
end
这是我在路线上安装它的方式:
mount API::Root => '/'
我收到的错误是:
'中的
routes.rb:45:in
块:未初始化的常量API(NameError)`这些文件的结构类似于
app/api/root.rb
,我在application.rb
中有以下代码要加载到文件中:config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
最佳答案
尝试将API代码的文件从app/api
移到app/api/api
。
从Grape's documentation:
Place API files into
app/api
. Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class. In our example, the file name location and directory forTwitter::API
should beapp/api/twitter/api.rb
.
因此,您的
API::Root
类的正确位置实际上是app/api/api/root.rb
。
关于ruby-on-rails - 为什么在Rails中安装路由会因 “uninitialized constant API”而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24833131/