Tag: active model serializers

ActiveModel :: Serializer无法正常工作

我的控制器中有一个客户端模型和一个方法,应该返回最近的客户端。 我正在使用ActiveModel :: Serializers但它不起作用。 class ClientSerializer < ActiveModel::Serializer attributes :id, :name, :path, :url def url client_url(object) end def path client_path(object) end end 控制器: def nearby_from_category @closest_clients = Client.from_category(params[:category]). activated.locateable.all_with_translation(@lang). by_distance(origin: remote_ip).limit(2) render json: @closest_clients.to_json(include: { translations: {only: [:name, :content]}, pictures: {only: :image} }) end JavaScript的: $(function () { $(“.nav_category”).hover(function () { var category_dropdown = $(this).children(“.category_dropdown”); […]

强制活动模型序列化程序返回关联

我有一个Active Model Serializer ,它具有以下function: class API::DashboardSerializer < ActiveModel::Serializer attributes :id, :name, :special def special x = object.check_ins.first prev = x.prev_ci_with_weigh_in end end special返回CheckIn类的记录,我希望它为该记录使用CheckInSerailizer 。 如何强制它使用special的CheckInSerializer ?

控制器中的索引方法不适用于活动模型序列化程序

我想发送一个包含我的bookmarks资源集合的json响应。 出于某种原因,主动模型序列化器不会按预期操纵我的json。 它只呈现默认响应。 这是app/serializers/bookmarks_serializer.rb BookmarksSerializer序列化app/serializers/bookmarks_serializer.rb class BookmarksSerializer < ActiveModel::Serializer attributes :id, :url, :title end 和我的Api::UsersController控制器方法,它在顶部有一个respond_to :jsonfilter def index user = User.find_by(username: params[:username]) @bookmarks = user.bookmarks render json: @bookmarks.to_json end 而不是只获取:id,:title和:url,我得到完整的默认哈希 我究竟做错了什么? 我使用rails 4.1.5和active_model_serializers gem的github版本。 gem ‘active_model_serializers’, github: ‘rails-api/active_model_serializers’ 仅供参考:我在同一个控制器中有一个show方法,它的序列化器可以很好地获取单个资源。 它只是索引方法中不呈现的集合。 请帮忙

如何通过Rails的Active模型序列化器呈现嵌套对象的父数据?

我正在研究Rails 5 api only app。 所以这是我的模型序列化器 class MovieSerializer < ActiveModel::Serializer attributes :id ,:name,:release_year,:story,:in_theater,:poster,:score,:user_count belongs_to :age_rating belongs_to :company has_many :categories has_many :movie_celebrities end class MovieCelebritySerializer < ActiveModel::Serializer attributes :id,:vacancy,:title belongs_to :celebrity end class CelebritySerializer < ActiveModel::Serializer attributes :id, :first_name, :last_name has_many :movie_celebrities end 我的控制器 class Api::V1::MoviesController < ApplicationController # GET /v1/movies/:id def show movie = Movie.find_by(id: […]

Rails ActiveModelSerializer将两个相同类型模型的列表组合成一个序列化响应,具有不同的名称

我有一个rails api,其中我使用Active Model Serializers(版本0.10.6)来序列化json响应。 我有两个数组,在端点中都是“FeatureRequest”类型,返回用户发出的请求列表,以及第二个用户被标记的请求列表。理想情况下,我想将响应序列化为看起来像这样: { “my_requests” : { …each serialized request… }, “tagged_requests” : { …each serialized request, using the same serializer” } } 有办法做到这一点吗? 这是我的相关控制器方法: def index_for_user user = User.includes(leagues: :feature_requests).find(params[:user_id]) # Find all requests that this user created @users_created_feature_requests = FeatureRequest.requests_for_user(user.id) @feature_requests_in_tagged_leagues = [] user.leagues.each do |league| @feature_requests_in_tagged_leagues << league.feature_requests end …some […]

Ember Data属于协会(JSON格式?)

我有两个模型’作者’和’发布者’(Rails),发布者有一个作者/作者所属的发布者关系。 我正确设置了Ember模型 – JS Fiddle – 以及当我手动推入商店时的关联。 但是,在请求/发布者索引时,只会创建发布者记录。 我尝试了几种类型的JSON响应: 出版商与作者 { “publishers”: [ { “id”: 1, “name”: “Test P 1”, “author”: 1 } ], “author”: { “id”: 1, “name”: “Test A 1”, “publisher”: 1 } } 有作者的出版商 { “publishers”: [ { “id”: 1, “name”: “Test P 1”, “author”: 1 } ], “authors”: [{ “id”: 1, […]

版本控制ActiveModel :: Serializer

我正在使用gem active_model_serializers ,我正面临着版本控制的一些问题。 控制器 在app/controllers/v1/contracts_controller.rb module V1 class ContractsController < ApiController def index @contracts = Contract.all render json: @contracts end end end 在app/controllers/v2/contracts_controller.rb module V2 class ContractsController < ApiController def index @contracts = Contract.all render json: @contracts end end end 串行器 在app/serializers/v1/contract_serializer.rb class ContractSerializer < ActiveModel::Serializer attributes :id end 在app/serializers/v2/contract_serializer.rb class ContractSerializer < ActiveModel::Serializer attributes […]

Active Model Serializer和Pundit在显示CRUD操作期间删除记录

好的,这里有些东西被严重破坏了…… 我正在为我的Rails 5 JSONAPI服务器使用Active Model Serializer和Pundit,为我的前端应用程序使用Ember。 我有User模型和User模型的Pundit策略,可以防止非作者查看未发表的故事和章节。 目前,我看到一个奇怪的问题,如下所示: 1. UserA creates StoryA, and two published chapters Chapter1 and Chapter2 2. UserA then creates two unpublished chapters Chapter3 and Chapter4 3. UserA logouts 4. UserB logins 5. UserB views the same story created by UserA 6. Server policy kicks in and scope the results to only […]

使用ActiveModel :: Serializer序列化具有属性的数组/关系

我想使用Active Model Serializers序列化关系,我想为这个关系设置一些’全局’属性(例如count): { users: { total: 12, page: 2, users: [{}, {}, {}, …] } } 我怎么能这样做?

ActiveModel Serializers:has_many在运行时有条件吗?

我使用rails(5.0.1)和active_model_serializers(0.10.2)。 我想以某种方式有条件地序列化has_many关联: class Question :question end class Response :responses end class QuestionSerializer < ActiveModel::Serializer attributes :id, :title, :created_at, :updated_at has_many :responses end class ResponseSerializer < ActiveModel::Serializer attributes :id, :title end 我使用jsonapi并查询http://localhost:3000/api/questions/1我收到此回复: 回应1 : { “data”: { “id”: “1”, “type”: “questions”, “attributes”: { “title”: “First”, “created-at”: “2017-02-14T09:49:20.148Z”, “updated-at”: “2017-02-14T13:55:37.365Z” }, “relationships”: { “responses”: { “data”: [ […]