active-model-serializers

Active Model Serializer and Custom JSON Structure

别等时光非礼了梦想. 提交于 2019-12-07 04:54:47
问题 I'm trying to use the Active Model Serializer gem with my API, although I am struggling with something I thought would be pretty simple. All my JSON responses are in a wrapped format, with every response having a top level message and status property, the data is within the content property. Every JSON response follows this format. Example { 'status': statuscode, 'message': message, 'content': { 'object':obj } } The contents of the "content" property is where I would like to place the output

has_many configuration for Ember-Data and Active Model Serializers with embedded IDs and sideloading

余生长醉 提交于 2019-12-07 03:15:05
问题 I know that Ember-Data is supposed to be compatible with Active Model Serializers by design, but they seem to be out of step on serializing has_many relationships with embedded IDs. For example, the serializer class PostSerializer < ActiveModel::Serializer embed :ids has_many :comments end produces the JSON { "post": { "comment_ids": [...] } } But the default configuration in Ember Data, App.Post = DS.Model.extend({ DS.hasMany('App.Comment'), }); App.Comment = DS.Model.extend(); expects the

Url Helpers in ActiveModelSerializer 0.10.0?

心已入冬 提交于 2019-12-06 16:49:28
问题 I know this version is still not officially released but I was checking out rc3 today and I noticed that I can no longer use Rails url helpers inside my serializers. In version 0.8.x, I could do the following: class BrandSerializer < BaseSerializer attributes :id, :name, :slug, :state attributes :_links def _links { self: api_v1_company_brand_path(object.company_id, object.id), company: api_v1_company_path(object.company_id), products: api_v1_company_brand_products_path(object.company_id,

ActiveModel::Serializers Gem - Versioned API Namespacing Issue

邮差的信 提交于 2019-12-06 10:14:46
问题 I'm new to Rails and Modules/Namespaces My Controller is namespaced like this: module Api module V1 class PostsController < ApiController And ActiveModel::Serializers put a "Serializers" folder in my app folder, and in it, I've created post_serializer.rb containing the following code: class PostSerializer < ActiveModel::Serializer attributes :id, :body, :category, end When I try to access the JSON response I get: NameError at /api/v1/posts uninitialized constant Api::V1::PostsController:

How to select serializer for nested objects with active_model_serializers

北战南征 提交于 2019-12-06 03:52:22
问题 I am using rails 4.0.0 and am looking for a way to serialize a custom object which contains predefined objects with these predefined object's serializers. Example: I have a model Student with a serializer StudentSerializer. I want to render a JSON object as follows: { user_type: "student" student: { id: 1 email: fake@email.com } } My serializer written for Student only serializes the email and id attributes. However when I call: render json: {user_type="student", student: stu} I get the full

Active Model Serializers : undefined method `url_for' for nil:NilClass

半城伤御伤魂 提交于 2019-12-05 20:27:19
I'm using active_model_serializers gem in my application to send high level json response. It does actually works fine but since I've install that gem, after few minutes of use, the application crash, showing the error above. Not sure if my code is related to it but somehow, I needed to send back some partial as well. Since it didn't seems to be supported by the gem, I did a work around : class AppSerializer < ActiveModel::Serializer def render_partial_to_json(options = {}) partial = options[:partial] || nil locals = options[:locals] || nil context = Rails.configuration.paths['app/views'] view

ActiveModel Serializer - Passing params to serializers

落爺英雄遲暮 提交于 2019-12-05 10:57:30
AMS version: 0.9.7 I am trying to pass a parameter to an ActiveModel serializer without any luck. My (condensed) controller: class V1::WatchlistsController < ApplicationController def index currency = params[:currency] @watchlists = Watchlist.belongs_to_user(current_user) render json: @watchlists, each_serializer: WatchlistOnlySerializer end My serializer: class V1::WatchlistOnlySerializer < ActiveModel::Serializer attributes :id, :name, :created_at, :market_value attributes :id def filter(keys) keys = {} if object.active == false keys end private def market_value # this is where I'm trying to

Active Model Serializer and Custom JSON Structure

好久不见. 提交于 2019-12-05 10:51:41
I'm trying to use the Active Model Serializer gem with my API, although I am struggling with something I thought would be pretty simple. All my JSON responses are in a wrapped format, with every response having a top level message and status property, the data is within the content property. Every JSON response follows this format. Example { 'status': statuscode, 'message': message, 'content': { 'object':obj } } The contents of the "content" property is where I would like to place the output of the Serializer. My lists of articles, etc. I cannot figure out how to do this though? Any help would

has_many configuration for Ember-Data and Active Model Serializers with embedded IDs and sideloading

岁酱吖の 提交于 2019-12-05 07:42:58
I know that Ember-Data is supposed to be compatible with Active Model Serializers by design, but they seem to be out of step on serializing has_many relationships with embedded IDs. For example, the serializer class PostSerializer < ActiveModel::Serializer embed :ids has_many :comments end produces the JSON { "post": { "comment_ids": [...] } } But the default configuration in Ember Data, App.Post = DS.Model.extend({ DS.hasMany('App.Comment'), }); App.Comment = DS.Model.extend(); expects the comments association to be serialized as comments: [...] without the _ids suffix (see the relationships

Url Helpers in ActiveModelSerializer 0.10.0?

折月煮酒 提交于 2019-12-04 23:52:38
I know this version is still not officially released but I was checking out rc3 today and I noticed that I can no longer use Rails url helpers inside my serializers. In version 0.8.x, I could do the following: class BrandSerializer < BaseSerializer attributes :id, :name, :slug, :state attributes :_links def _links { self: api_v1_company_brand_path(object.company_id, object.id), company: api_v1_company_path(object.company_id), products: api_v1_company_brand_products_path(object.company_id, object.id) } end end But this is a no go in the new version. What's the best way of resolving this so that