标签: active-model-serializers

Active Model Serializer无法使用json_api适配器

我正在尝试使用自定义序列化程序来处理序列化程序中的关系并启用json_api适配器.但是,关系没有正确序列化(或者更好,完全没有显示/序列化).

PostController.rb

def index
  render json: Post.all, each_serializer: Serializers::PostSerializer
end
Run Code Online (Sandbox Code Playgroud)

串行

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
Run Code Online (Sandbox Code Playgroud)

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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,关系未得到满足, …

ruby json ruby-on-rails active-model-serializers

1
推荐指数
1
解决办法
2451
查看次数

活动模型序列化器-路由助手的未定义方法

我正在用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 …
Run Code Online (Sandbox Code Playgroud)

active-model-serializers

1
推荐指数
1
解决办法
2828
查看次数

条件除外,仅包含在 Rails 的渲染 json 中

渲染时可以有条件的except,only或include选项吗?所以,就像下面的例子:

render json: @post,
except: [:author]
Run Code Online (Sandbox Code Playgroud)

是否有可能有条件的除外选项或类似的选项?

理想情况下,有条件的方式可以让我处理许多不同的条件和情况。

就像可能是这样的:

render json: @post,
except: return_excluded_keys
Run Code Online (Sandbox Code Playgroud)

return_excluded_keys 函数可以返回需要排除的键。

我正在使用 Rails 4.2.6 和 Active Model Serializers 0.9.3。

ruby-on-rails active-model-serializers

1
推荐指数
1
解决办法
3653
查看次数

Rails:我可以一起使用 elasticsearch-model 和 active_model_serializers 吗?

我正在 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 …
Run Code Online (Sandbox Code Playgroud)

ruby json ruby-on-rails elasticsearch active-model-serializers

1
推荐指数
1
解决办法
1026
查看次数

Active Model Serializers has_many association

我正在使用宝石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
Run Code Online (Sandbox Code Playgroud)

控制器:

def index
  @project_groups = ProjectGroup.all.includes(:projects)
  render json: @project_groups, include: 'projects'
end
Run Code Online (Sandbox Code Playgroud)

我收到以下回复:

{
  "data": [
    {
      "id": "35",
      "type": "project_groups",
      "attributes": {
        "name": "sdsdf",
        "description": null
      },
      "relationships": {
        "projects": {
          "data": [
            {
              "id": "1",
              "type": "projects"
            },
            {
              "id": "2",
              "type": "projects"
            }
          ]
        }
      }
    }
  ],
  "included": …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails activemodel active-model-serializers

1
推荐指数
1
解决办法
5271
查看次数

Rails活动模型序列化器-属性别名

我正在将Angular 2应用程序与Rails 5.1 API后端集成在一起。

我的Angular模型中有一个Comment具有messagedate属性的模型。但是,在我的Rails模型中Comment,连同message我拥有的属性created_at和via updated_at自动提供的属性。ActiveRecord::Migrationt.timestamps

我使用ActiveModel::Serializer会对API反应变量和更好地控制我comment_serializer我想包括date在地方的created_at在每个评论API响应GET /commentsGET /comments/:id等等,这样的角度模式不必改变-我怎么去这个假设这是正确的做法?

另外,当我从Angular客户端添加注释时,我希望将模型中的date属性Comment映射到Rails created_at中的相应Comment模型中-我该如何处理?

ruby-on-rails active-model-serializers angular

1
推荐指数
1
解决办法
1328
查看次数

Rails 序列化器 0.10 中的条件属性和方法

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
Run Code Online (Sandbox Code Playgroud)

在这里,我想为控制器的基本操作设置条件。

例如,我想仅发送索引操作的条件属性,而不发送其他操作的条件属性。

但据我所知,rails "active_model_serializers", "~> 0.10.0" 并没有给出任何这样的东西。

serialization ruby-on-rails active-model-serializers

1
推荐指数
1
解决办法
2474
查看次数

Rails:非ActiveRecord模型需要包含ActiveModel :: Serializers,还是只响应#as_json?

使用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
Run Code Online (Sandbox Code Playgroud)

这应该按预期工作,还是我还需要包含ActiveModel::Serializers::JSON?我很难搞清楚as_json/ to_json方法通常定义的位置以及Rails何时在不同情况下自动调用哪些方法...

感谢您的任何见解!

json ruby-on-rails active-model-serializers

0
推荐指数
1
解决办法
1142
查看次数

Ember数据中验证错误的预期格式(使用ActiveModel :: Serializers)

不幸的是,ActiveModel :: Serializers目前不支持验证错误,尽管它们被安排为1.0.在那之前,我必须破解我自己的解决方案.大问题?我不知道Ember Data的ActiveModelAdapter期望这些错误的格式是什么.我试过简单地传递errors属性,但是Ember Data没有接受它:

class MySerializer < ActiveModel::Serializer
  attributes :errors
end
Run Code Online (Sandbox Code Playgroud)

那么我该怎么办呢?

ember.js ember-data active-model-serializers

0
推荐指数
1
解决办法
967
查看次数

Active Model Serializer格式化JSON显示

我在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"}]}}}}
Run Code Online (Sandbox Code Playgroud)

我如何使其看起来像:

{
    "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"
                }]
            }
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

api json ruby-on-rails active-model-serializers

0
推荐指数
1
解决办法
502
查看次数

活动模型序列化器:如何将选项传递给集合?

如果我有一个集合,比如 Widgets,并且我正在使用 Active Model Serializers 来序列化 Widgets 集合,那么如何将 instance_options 传递给集合?

render json: @widgets, count: 40
Run Code Online (Sandbox Code Playgroud)

我尝试了上面的方法,但似乎无法进入count: 40我的instance_options. 我错过了什么吗?

ruby-on-rails active-model-serializers

0
推荐指数
1
解决办法
3177
查看次数