Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 将活动模型序列化器与rails命名空间一起使用_Ruby On Rails 4_Activemodel_Active Model Serializers - Fatal编程技术网

Ruby on rails 4 将活动模型序列化器与rails命名空间一起使用

Ruby on rails 4 将活动模型序列化器与rails命名空间一起使用,ruby-on-rails-4,activemodel,active-model-serializers,Ruby On Rails 4,Activemodel,Active Model Serializers,我在这种情况下,, 在我的控制器中,我调用: render json: @customers_filtered, serializer: User::CustomerSerializer 我的序列化程序: class User::CustomerSerializer < ActiveModel::Serializer attributes :id,:firstname end 如果我从序列化程序中剪切用户::,它将找不到从中读取属性的模型 有没有办法修复di行为?我可以在序列化程序

我在这种情况下,, 在我的控制器中,我调用:

render json: @customers_filtered, serializer: User::CustomerSerializer
我的序列化程序:

class User::CustomerSerializer < ActiveModel::Serializer
  attributes :id,:firstname
end
如果我从序列化程序中剪切
用户::
,它将找不到从中读取属性的模型


有没有办法修复di行为?我可以在序列化程序上设置模型名称吗?

尝试将序列化程序文件放在名为“用户”的子文件夹中,将类包装在
模块用户中
并将类重命名为
类CustomerSerializer
。这可能是因为Rails的auto-require/include-magic。它给了我:未定义的方法'id',你是说#ids但这没有意义,因为我的客户有id…我无法理解这是什么关系…每当您看到“::ActiveRecord_Relation:xxx”请记住,该关系中可以有多个id,这意味着您不需要关系的id,而需要该关系中元素的id。ActiveRecord方便地为您提供了获取ID数组的方法“ids:)这意味着我需要为具有一组模型的类动作索引和只有一个模型的类动作显示使用不同的序列化程序?
Unable to autoload constant CustomerSerializer, expected /var/www/qbvault/app/serializers/customer_serializer.rb to define it