- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将一个对象传递给一个返回所有记录的序列化程序,当对象返回空数组时就是出现此错误的时间。
def booked_cars
if params["id"].present?
@customer = Customer.find(params["id"].to_i)
@booked_cars = @customer.bookings.where(cancelled: false).collect{|c| c.used_car}
render json: @booked_cars, each_serializer: UsedCarSerializer
end
end
我希望它给出对象数组或空数组,而不是给出参数错误(ArgumentError(无法从集合类型推断根键。请指定根或每个_serializer 选项,或呈现 JSON 字符串):)
最佳答案
尝试按照 active_model_serializer 的错误响应中的指定添加 serializer
选项或 root
选项。
因为序列化器从集合中获取根。
@customer = Customer.find(params["id"].to_i)
render json: @customer
在上面的例子中,序列化器会像下面这样响应,
{
"customer": #root
{
# attributes ...
}
}
因为对象不是集合,所以根是单数形式(customer)。
@customers = Customer.where(id: ids) # ids is an array of ids.
render json: @customer
在上面的例子中,序列化器会像下面这样响应,
{
"customers": #root
{
# attributes ...
}
}
因为对象不是集合,所以词根是复数形式(customers)。
序列化器会根据对象的类(ActiveRecord || ActiveRecordCollection)添加root。
如果对象是空数组,序列化程序无法预测将哪个用作根。所以我们需要指定root 或serializer 选项。
render json: @customer, root: 'customer'
或
render json: @customer, serializer: UsedCarSerializer
注意:事件模型序列化程序从对象类或序列化程序选项中检测序列化程序。
关于ruby-on-rails - 如何修复 'ArgumentError: Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57427120/
我使用 Facebook 的 Infer 检查我的 android 代码 infer -- ./gradlew build after 8 minutes Fatal error: exception
来自 JEP 286 ,我们看到我们将能够在 JDK 10 (18.3) 中利用本地类型推断 (var)。 JEP 声明以下编译,这是预期的: var list = new ArrayList();
我可以拥有 val composeFunction = remember { mutableStateOf ({}) } 我可以拥有 val composeFF = @Composable { Tex
我真的不知道如何解决这个问题(错误:不兼容的类型:推断的类型不符合推断的上限:INT#1 上限:Giocatore[],Parcelable where INT# 1 是交集类型:INT#1 exte
直到今天,在我们将 Visual Studio 2017 更新到最新的 15.3 之后,我们的 UWP 应用程序中的以下代码一直运行良好。 private void Test() { var
我有以下代码,我不明白: type Msg = Left | Right content : Html Msg content = p [] [] p的类型签名: p : List (
类型推断的局限性是什么?哪些类型的系统没有通用的推理算法? 最佳答案 Joe Wells表明系统F的类型推断是不确定的,System F是最基本的多态Lambda演算,由Girard和Reynolds
我在构建时不断收到以下错误 Karma 运行失败:未定义 ERROR [karma]: ReferenceError: Strict mode forbids implicit creation of
在编程语言中,推断类型和动态类型有什么区别?我知道动态类型,但不知道动态类型与推断类型有何不同?有人可以通过一些例子提供解释吗? 最佳答案 推断类型 = 在编译时设置一次。实际上,推断部分只是为了节省
我有一个类: class MyClass{ enum Choices : int { First, Last }; template
考虑下面的类 class SomeBaseClass { ... } 以及以下使用infer关键字的条件类型 type ExtractInner = T extends SomeBaseCla
这个问题在这里已经有了答案: In TypeScript, how to get the keys of an object type whose values are of a given typ
例如,Agda 允许我这样写: open import Data.Vec open import Data.Nat myVec : Vec ℕ _ myVec = 0 ∷ 1 ∷ 2 ∷ 3 ∷ []
我正在使用: $ coqtop -v The Coq Proof Assistant, version 8.4pl5 (February 2015) compiled on Feb 06 2015 1
我知道像 Haskell 这样的语言是静态类型的并且有类型推断。但是是否存在具有全局类型推断的非函数式语言,相当于具有类型推断和结构类型的 C 之类的东西。 最佳答案 OCaml 是我所知道的唯一一种
鉴于: sub abc(Int $n) { say $n } 如果我们将类型为Str的变量传递给abc,则会得到编译时错误: my Str $s = "123"; abc $s; 如果传递包含字符
序言:这是基于@Travis Brown 的 macro based solution复制案例类属性。 鉴于: trait Entity[E def id: Int def withId(i
为什么可以推断闭包表达式的参数类型和返回类型,而不是 Rust 中的函数? 最佳答案 这只是一个设计决定:Rust 使用局部类型推断,而不是全局类型推断。理论上可以进行全局类型推断,但为了便于调试,R
我想使用的类是 System.Web.Mvc.DependencyResolver。以下作品: using System.Web.Mvc; ... var x = DependencyResolver
我声明了以下字典: private readonly Dictionary dictionary; 我有一个导致编译器错误的方法: public IQueryable Find(Func ex
我是一名优秀的程序员,十分优秀!