【发布时间】:2016-12-08 15:01:21
【问题描述】:
我正在处理一个rails-api 项目,我正在使用active model serialiazer。但不幸的是,它没有按预期工作。这是我的V1::HuntsController
class V1::HuntsController < V1::MainController
def index
render json: Hunt.all
end
end
我的狩猎序列化器看起来像这样
class HuntSerializer < ActiveModel::Serializer
belongs_to :user
attributes :id,:title,:picture_url,:clue
private
def picture_url
object.picture.url
end
end
但是在我的response 中,我从hunt 获得了所有属性。我尝试显式定义序列化程序以避免版本控制问题。
render json: {data: Hunt.all } ,each_serializer: HuntSerializer
但似乎没有任何效果。在我可以看到的日志中,
[active_model_serializers] Rendered V1::HuntSerializer with Hash (32.36ms)
这里发生了什么。任何帮助将不胜感激。
【问题讨论】:
标签: ruby-on-rails json active-model-serializers