在控制器指数法不工作的主动型串行 [英] index method in controller not working for active model serializer

查看:120
本文介绍了在控制器指数法不工作的主动型串行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给我的包含书签资源集合的 JSON 响应。出于某种原因,如预期主动型串行只是没有操纵我的JSON。这只是渲染默认响应。

I want to send a json response containing a collection of my bookmarks resource. For some reason active model serializer is just not manipulating my json as expected. It's only rendering the default response.

下面是应用程序/串行器/ bookmarks_serializer.rb

class BookmarksSerializer < ActiveModel::Serializer
  attributes :id, :url, :title
end

和我的 API :: UsersController内我的控制器方法具有的respond_to:JSON 过滤器顶部

def index
  user = User.find_by(username: params[:username])
  @bookmarks = user.bookmarks
  render json: @bookmarks.to_json
end

而不是只得到的:ID,:title和:URL,我得到充分的默认散列

Instead of getting only :id, :title and :url, I am getting the full default hash

我是什么做错了吗?我使用栏杆4.1.5 和GitHub的版本 active_model_serializers
宝石。

What am I doing wrong? I am using rails 4.1.5 and the github version of the active_model_serializers gem.

gem 'active_model_serializers', github: 'rails-api/active_model_serializers'

FYI:我有相同的控制器内的表演方法,它是串行拿起单一的资源就好了。它只是在未呈现指数方法集合。请帮助

FYI: I have a show method inside the same controller and it's serializer picks up the single resource just fine. It's just the collection in the index method that is not rendering. Please help

推荐答案

您正在使用主动模式串行的主分支。 <一href=\"https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/array_serializer.rb\"相对=nofollow> 为4天前添加,并且不接受任何参数这可能是错误的数组序列化。您code应该对做工精良0-9稳定。尝试:

You are using the master branch of active model serializers. The array serializer was added 4 days ago and is not accepting any arguments which is probably erroneous. Your code should work well against 0-9-stable. Try:

创业板active_model_serializers',github上:Rails的API / active_model_serializers',分支:'0-9稳定

这篇关于在控制器指数法不工作的主动型串行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆