gpt4 book ai didi

ruby-on-rails - 为什么在Rails中安装路由会因 “uninitialized constant API”而失败?

转载 作者:行者123 更新时间:2023-12-04 07:34:54 32 4
gpt4 key购买 nike

我正在开发一个包含使用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 for Twitter::API should be app/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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com