我正在尝试使用自定义序列化程序来处理序列化程序中的关系并启用json_api适配器.但是,关系没有正确序列化(或者更好,完全没有显示/序列化).
PostController.rb
def index
  render json: Post.all, each_serializer: Serializers::PostSerializer
end
串行
module Api
  module V1
    module Serializers
      class PostSerializer < ActiveModel::Serializer
        attributes :title, :id
        belongs_to :author, serializer: UserSerializer
        has_many :post_sections, serializer: PostSectionSerializer
      end
    end
  end
end
JSON输出:
{
    "data": [
        {
            "attributes": {
                "title": "Test Title"
            },
            "id": "1",
            "relationships": {
                "author": {
                    "data": {
                        "id": "1",
                        "type": "users"
                    }
                },
                "post_sections": {
                    "data": [
                        {
                            "id": "1",
                            "type": "post_sections"
                        }
                    ]
                }
            },
            "type": "posts"
        }
    ]
}
正如您所看到的,关系未得到满足, …
我正在用rails-apigem 构建一个新的应用程序。我用active_model_serializers来自定义JSON响应。在尝试向url序列化器添加属性时,出现错误,指示资源的路由帮助器方法未定义。要遵循的相关代码:
# Routes
Rails.application.routes.draw do
  namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      resources :tasks, except: [:new, :edit]
    end
  end
end
# Controller - api/v1/tasks_controller.rb
class Api::V1::TasksController < ApplicationController
  before_action :set_task, only: [:show, :update, :destroy]
  # GET /tasks
  # GET /tasks.json
  def index
    @tasks = Task.all
    render json: @tasks
  end
  # GET /tasks/1
  # GET /tasks/1.json
  def show
    render json: @task
  end
  # POST /tasks
  # POST /tasks.json
  def create …渲染时可以有条件的except,only或include选项吗?所以,就像下面的例子:
render json: @post,
except: [:author]
是否有可能有条件的除外选项或类似的选项?
理想情况下,有条件的方式可以让我处理许多不同的条件和情况。
就像可能是这样的:
render json: @post,
except: return_excluded_keys
return_excluded_keys 函数可以返回需要排除的键。
我正在使用 Rails 4.2.6 和 Active Model Serializers 0.9.3。
我正在 Rails 中构建一个 JSON API,我想使用 Elasticsearch 来加快响应速度并允许搜索。
我刚刚为我的第一个模型实现了 elasticsearch-rails Gem,我可以成功地从控制台查询 ES。
现在我想让 API 消费者可以使用结果,例如,对 /articles/index.json?q="blah" 的 GET 请求将从 ES 检索匹配的文章并根据 JSON:API 标准呈现它们。
是否可以使用 rails active_model_serializers gem 来实现这一点?我问是因为(与 jbuilder 相比)JSON:API 格式已经得到处理。
编辑:这是我目前的立场:
在我的模型中,我有以下内容:
require 'elasticsearch/rails'
require 'elasticsearch/model'
class Thing < ApplicationRecord
    validates :user_id, :active, :status, presence: true
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
    index_name Rails.application.class.parent_name.underscore
    document_type self.name.downcase
    settings index: { number_of_shards: 1, number_of_replicas: 1 } do 
        mapping dynamic: 'strict' do 
            indexes :id, type: :string
            indexes :user_id, type: :string
            indexes :active, type: :boolean …ruby json ruby-on-rails elasticsearch active-model-serializers
我正在使用宝石active_model_serializers.
串行器:
class ProjectGroupSerializer < ActiveModel::Serializer
  attributes :id, :name, :description
  has_many :projects
end
class ProjectSerializer < ActiveModel::Serializer
  attributes :id, :name, :description
  belongs_to :project_group
end
控制器:
def index
  @project_groups = ProjectGroup.all.includes(:projects)
  render json: @project_groups, include: 'projects'
end
我收到以下回复:
{
  "data": [
    {
      "id": "35",
      "type": "project_groups",
      "attributes": {
        "name": "sdsdf",
        "description": null
      },
      "relationships": {
        "projects": {
          "data": [
            {
              "id": "1",
              "type": "projects"
            },
            {
              "id": "2",
              "type": "projects"
            }
          ]
        }
      }
    }
  ],
  "included": …我正在将Angular 2应用程序与Rails 5.1 API后端集成在一起。
我的Angular模型中有一个Comment具有message和date属性的模型。但是,在我的Rails模型中Comment,连同message我拥有的属性created_at和via updated_at自动提供的属性。ActiveRecord::Migrationt.timestamps
我使用ActiveModel::Serializer会对API反应变量和更好地控制我comment_serializer我想包括date在地方的created_at在每个评论API响应GET /comments,GET /comments/:id等等,这样的角度模式不必改变-我怎么去这个假设这是正确的做法?
另外,当我从Angular客户端添加注释时,我希望将模型中的date属性Comment映射到Rails created_at中的相应Comment模型中-我该如何处理?  
class Api::V1::BookSerializer < ActiveModel::Serializer
  attributes :id, :status, :name, :author_name, :published_date
  attributes :conditional_attributes if condition_1?
  belongs_to :user if condition_2?
end
在这里,我想为控制器的基本操作设置条件。
例如,我想仅发送索引操作的条件属性,而不发送其他操作的条件属性。
但据我所知,rails "active_model_serializers", "~> 0.10.0" 并没有给出任何这样的东西。
使用Rails 3.2,我正在研究API支持的模型(不是ActiveRecord).我希望能够to_json在Rails控制器中调用此模型.在阅读了一堆ActiveModel文档后,我仍然不清楚一件事:
鉴于这样的模型:
class MyModel
  attr_accessor :id, :name
  def initialize(data)
    @id = data[:id]
    @name = data[:name]
  end
  def as_json
    {id: @id, name: @name}
  end
end
这应该按预期工作,还是我还需要包含ActiveModel::Serializers::JSON?我很难搞清楚as_json/ to_json方法通常定义的位置以及Rails何时在不同情况下自动调用哪些方法...
感谢您的任何见解!
不幸的是,ActiveModel :: Serializers目前不支持验证错误,尽管它们被安排为1.0.在那之前,我必须破解我自己的解决方案.大问题?我不知道Ember Data的ActiveModelAdapter期望这些错误的格式是什么.我试过简单地传递errors属性,但是Ember Data没有接受它:
class MySerializer < ActiveModel::Serializer
  attributes :errors
end
那么我该怎么办呢?
我在Rails应用程序中使用Active模型序列化程序,我想重构显示内容:
每当我访问http:// localhost:3000 / api / users / 1时,我都会看到:
{"data":{"id":"1","type":"users","attributes":{"username":"Iggy1"},"relationships":{"items":{"data":[{"id":"1","type":"items"},{"id":"7","type":"items"}]},"lists":{"data":[{"id":"1","type":"lists"},{"id":"8","type":"lists"},{"id":"14","type":"lists"},{"id":"15","type":"lists"},{"id":"17","type":"lists"}]}}}}
我如何使其看起来像:
{
    "data": {
        "id": "1",
        "type": "users",
        "attributes": {
            "username": "Iggy1"
        },
        "relationships": {
            "items": {
                "data": [{
                    "id": "1",
                    "type": "items"
                }, {
                    "id": "7",
                    "type": "items"
                }]
            },
            "lists": {
                "data": [{
                    "id": "1",
                    "type": "lists"
                }, {
                    "id": "8",
                    "type": "lists"
                }, {
                    "id": "14",
                    "type": "lists"
                }, {
                    "id": "15",
                    "type": "lists"
                }, {
                    "id": "17",
                    "type": "lists"
                }]
            }
        }
    }
} …如果我有一个集合,比如 Widgets,并且我正在使用 Active Model Serializers 来序列化 Widgets 集合,那么如何将 instance_options 传递给集合?
render json: @widgets, count: 40
我尝试了上面的方法,但似乎无法进入count: 40我的instance_options. 我错过了什么吗?