Ruby on rails 带数组的用户实体
我想知道Grape实体是否可以用于渲染散列数组,我想我记得它可以工作,但不知何故,我现在无法让它工作,我是否犯了一些明显的错误?这是我的实体:Ruby on rails 带数组的用户实体,ruby-on-rails,grape-api,grape-entity,Ruby On Rails,Grape Api,Grape Entity,我想知道Grape实体是否可以用于渲染散列数组,我想我记得它可以工作,但不知何故,我现在无法让它工作,我是否犯了一些明显的错误?这是我的实体: class V1::Entities::Searchresult < Grape::Entity expose :_type, as: :type expose :_id, as: :id expose :_score, as: :score expose :highlight end “result['hits']['h
class V1::Entities::Searchresult < Grape::Entity
expose :_type, as: :type
expose :_id, as: :id
expose :_score, as: :score
expose :highlight
end
“result['hits']['hits']”由包含数据的10个哈希值填充。数据是存在的。然而,当我看到结果时,我得到:
[
{
"type": null,
"id": null,
"score": null,
"highlight": null
},
{
"type": null,
"id": null,
"score": null,
"highlight": null
},
......
是我做错了什么,还是这根本不可能。我似乎找不到任何关于array toppic的文档
干杯
Tom我发现了错误,Grape::Entity::Delegator::HashObject无法处理具有字符串键而非符号的哈希。它无法提取值
data = []
result['hits']['hits'].each do |item|
data << item.symbolize_keys
end
present data, with: V1::Entities::Searchresult, :params => params
将解决整个问题,而不仅仅是使用
object[attribute]
读取属性
object[attribute] || object[attribute.to_s]
object[attribute]