Questions tagged [active-model-serializers]

The purpose of `ActiveModel::Serializers` is to provide an object to encapsulate serialization of `ActiveModel` objects, including `ActiveRecord` objects.

Purpose

The purpose of ActiveModel::Serializers is to provide an object to encapsulate serialization of ActiveModel objects, including ActiveRecord objects.

Serializers know about both a model and the current_user, so you can customize serialization based upon whether a user is authorized to see the content.

In short, serializers replaces hash-driven development with object-oriented development.

Documentation

See the github project's README

739 questions
11
votes
3 answers

Serialize permissions (e.g. CanCan) with active_model_serializers

How do I serialize permissions with active_model_serializers? I don't have access to current_user or the can? method in models and serializers.
Jo Liss
  • 30,333
  • 19
  • 121
  • 170
10
votes
7 answers

How to use devise current_user in Active Model Serializers

I'm using Active Model Serializer 0.10.7 in rails 5 and I wanna know how to access devise current_user in serializer. current_user is supposed to be set for scope by default. according to…
Otani Shuzo
  • 1,118
  • 1
  • 11
  • 22
10
votes
2 answers

JSON rendering with activemodel serializer in Rails

I just wonder how the rails render the model objects with activemodel-serializer in JSON. I installed activemodel-serializer but the output is something different. The ideal is something like: "products": [ { "id": 1, "title": "Digital…
Toshi
  • 6,012
  • 8
  • 35
  • 58
10
votes
2 answers

how to dynamic add attributes on Active Model Serializers

I want to decide numbers of attributes to output in my controller. But I have no idea have to do it? controller.rb respond_to do |format| if fields # less attributes : only a,c elsif xxx # default attributes else #…
newBike
  • 14,385
  • 29
  • 109
  • 192
10
votes
4 answers

Serializing the errors hash in ActiveModel::Serializer

I'm using ActiveModel::Serializer to customize the JSON responses for my API. This works fine in most cases, except when it fails to save a model successfully. For example, def create def create book = Book.new(book_params) book.save …
10
votes
3 answers

Asset Pipeline in Active Model Serializers

I'm attempting to include an image asset pipeline url in my model serializer output by including ActiveView::Helpers: class PostSerializer < ActiveModel::Serializer include ActiveView::Helpers attributes :post_image def post_image …
9
votes
2 answers

How do I select which attributes I want for active model serializers relationships

I am using the JSONAPI format along with Active Model Serializers to create an api with rails-api. I have a serializer which shows a specific post that has many topics and currently, under relationships, lists those topics. It currently only lists…
alassiter
  • 505
  • 1
  • 5
  • 15
9
votes
5 answers

How to return all attributes of an object with Rails Serializer?

I have a simple question. I have a seriaizer that looks like this: class GroupSerializer < ActiveModel::Serializer attributes :id, :name, :about, :city end The problem is that, whenever I change my model, I have to add/remove attributes from…
Salah Saleh
  • 793
  • 9
  • 29
9
votes
4 answers

active model serializer not working with rails-api gem

I am using rails-api gem in my project for json api, and for that purpose I used active model serializer gem for serializing my objects but some how the objects are not being serialized using active model serializer. I have a MessageSerializer…
Gagan
  • 4,278
  • 7
  • 46
  • 71
9
votes
2 answers

serialize date attributes

I am using active_model_serializers and ember.js. One of my models has a date attribute. In rails date attributes are serialized in the format of "YYYY-MM-DD". The problem; when ember-data de-serializes the date using the javascript Date…
Aaron Renoir
  • 4,283
  • 1
  • 39
  • 61
8
votes
1 answer

Difference between django.core serializers and Django Rest Framework serializers

I am now learning Django and I just heard about Django Rest Framework (DRF). I was wondering what is the difference between the django.core serializers and the rest_framework serializers. Yes, I know DRF is for APIs.
8
votes
3 answers

ActiveModel Serializer - Passing params to serializers

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 =…
Rachel
  • 157
  • 2
  • 8
8
votes
2 answers

ActiveModel::Serializer::CollectionSerializer::NoSerializerError in active_model_serializer 0.10.0.rc5

I'm using active_model_serializer 0.10.0.rc5 and grape gem for the api. I've a post endpoint like this : class V1::Endpoints::Posts < Grape::API resource :posts do desc 'Returns a list of posts.' # serializing array get '',…
Rupali
  • 311
  • 3
  • 5
8
votes
3 answers

Ruby API response as lower camel case

I'm trying to return the API response as lowerCamelCase but it is not working, I need to do this for all my Controllers/fields so I need a solution for entire project. I've tried a lot of stuff, including this…
8
votes
4 answers

In Rails, why am I getting a "204 - No Content" response for my update/PATCH/PUT, using Active Model Serializers?

This code is for a UserList (a user can create a User To-Do List). This particular resource does not hold the list items, but just the title of the list, and the type of list. class Api::V1::UserListsController < ApplicationController respond_to…
chris P
  • 6,359
  • 11
  • 40
  • 84
1 2
3
49 50