我有一个命名空间模型,如下所示:
module A
module B
class C
end
end
end在app/models/a/b/c.rb中。
然后是一个控制器
module API
module V2
module B
class CController
include ActionController::RespondWith
respond_to :json
end
end
end
end在app/controllers/api/v2/b/c_controller.rb中
命名空间文件通过config.autoload_paths += Dir["#{Rails.root}/app/*/**/"]自动加载到application.rb中。
Cors使用以下配置:
config.middleware.insert_after 0, Rack::Cors, logger: Rails.logger do
allow do
origins '*'
resource '*',
headers: :any,
methods: %i[get options post put patch delete]
end
end现在,当我将参数作为表单数据发送到create操作时,一切都很正常。当我用JSON字符串+ Content-Type application/json头发出同样的请求时,我得到这个错误:
LoadError: Unable to autoload constant C, expected app/models/a/b/c.rb to define it
Rails 5.2.0,Ruby 2.5.1
谁能帮上忙,或者有什么想法?
发布于 2018-08-13 18:29:20
我也犯了同样的错误,我刚刚成功地找出了这个问题的原因。这是因为抽象控制器试图包装您的参数,并将您的控制器类名转换为模型名。默认情况下,在config/initializers/rapparameters.rb文件中启用json请求的params-wrapper。但是您也可以在控制器级别禁用此功能,方法是在控制器中定义下一行:
wrap_parameters format: []https://stackoverflow.com/questions/51576303
复制相似问题