gpt4 book ai didi

ruby-on-rails - 无法在模块中的 Rails 4 模型中自动加载常量 API Controller

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:01 24 4
gpt4 key购买 nike

我正在尝试为模块中定义的模型创建一个 api Controller

模型类:

module Reports
class Report < ActiveRecord::Base
end
end

API Controller :

class API::V2::ReportsController < API::V2::BaseController

end

路线:

namespace :api, defaults: { format: :json } do
namespace :v2 do
resources :reports
end
end

尝试调用 api/v2/reports 时出现的错误是:

LoadError (Unable to autoload constant Report, expected /.../app/models/reports/report.rb to define it):

有没有办法解决这个问题,让 api Controller 寻找 Reports::Report 而不是 Report?

最佳答案

您的 Controller 需要以某种方式包含Reports:

module Api
module V2
module Reports
class Report < API::V2::BaseController

your controller actions

end
end
end
end

除此之外,我认为如果您使用 Reports::Report 会造成混淆?

关于ruby-on-rails - 无法在模块中的 Rails 4 模型中自动加载常量 API Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045211/

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