我正在研究用RocketPants制作的 Rails API 。对于 JSON 序列化,我使用active_model_serializers,对于 OAuth - Doorkeeper。
问题在于current_user访问  class UserSerializer < ActiveModel::Serializer. 错误:
NameError (undefined local variable or method `request' for #<UserSerializer:0xb5892118>)
current_user助手使用这个片段:
User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
并且doorkeeper_token是:
def doorkeeper_token
    methods = Doorkeeper.configuration.access_token_methods
    @token ||= OAuth::Token.authenticate request, *methods
end
据我发现,request中没有可访问的对象Serializer。我怎样才能使其易于访问?还是应该有其他的方式来实现current_user?  
提前致谢
我已将活动模型序列化器 gem 添加到项目中,它破坏了一堆东西,我们的一个 api 有一个我需要保留的非常特定的格式,不幸的是,我似乎无法获得遗留行为。
\n\n#Models\nclass Parent < ActiveRecord::Base\n  attr_accessable, :id, :name, :options\n  has_many :children\nend\n\nclass Child < ActiveRecord::Base\n  attr_accessable, :id, :name\nend\n\n#Controller\nclass ParentsController < ApplicationController\n\n  respond_to :json\n\n  def index\n    #Was\n    @parents = Parent.all\n    respond_with @parents, :include => [:children]\n\n    #Is (and is not working)\n    @parents = Parent.includes(:children)\n    respond_with @parents, each_serializer: ::ParentsSerializer, root: false  #Not working\n  end\n...\nend\n\n#Serializer\nclass ParentSerializer < ActiveModel::Serializer\n  attrs = Parent.column_names.map(&:to_sym) - [:options]\n  attributes(*attrs)\n  has_many :children\n\n  def filter(keys)\n    keys.delete :children unless object.association(:children).loaded?\n    keys.add :options\n    keys\n  end\nend\n[\n\xc2\xa0\xc2\xa0{\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0"parent": …我有以下序列化器/serializers/api/club_serializer.rb:
class Api::ClubSerializer < ActiveModel::Serializer
  cached
  attributes :id, :name, :created_at
end
以及 /controllers/api/clubs_controller.rb 下的以下方法
module Api
  class ClubsController < BaseController
    include ActionController::ImplicitRender
    include ActionController::MimeResponds
    # GET /clubs
    def index
      @clubs = Club.all
      render json: @clubs, serializer: ClubSerializer
    end
这似乎无法正常工作,因为我删除了名称,但它仍然显示所有字段的名称。
我该如何更改它才能正常工作?
我们有一个使用 Grape 和 Active Model Serializers 0.8 构建的 API。现在我们想使用 0.10 中的所有缓存优点,因此正在迁移到新的向后不兼容版本。
目前有两个问题:
似乎不可能使用self.root=内部序列化器重新定义根密钥。例如,我们已经有了SimpleUserSerializer,我们想要根密钥user而不是simple_user。解决方案是在渲染序列化器时指定根,但随后我们需要在很多地方进行更改。有没有办法重新定义此序列化器的根键,无论它在何处/如何渲染?
embed :ids, include: true选项不受支持,可能应该通过适配器来实现。是否有计划为遗留项目发布或维护 0.8 兼容适配器?
任何有关迁移的指导都会有所帮助,因为我找不到任何官方文档。
我有像这样的序列化器
class UserSeriazer < ActiveModel::Serializer
  attributes :name, :email
end
生成的 json 如下
{
  name: 'james'
  email: 'j@sample.com'
}
我如何在自定义键下包装上面的json 项目customer
{
  customer:
    {
      name: 'james'
      email: 'j@sample.com'
    }
}
我尝试了诸如属性:名称,:电子邮件,密钥::客户之类的东西,但没有用。提前致谢。
I have some data that in a tree/hierarchal model for (climbing) Areas, such that they relate via parent and child areas.
Using the JSON API adapter with my Active Model Serializer,
class AreaSerializer < ActiveModel::Serializer
  attributes :id, :name, :description, :location, :slug
  belongs_to :user
  belongs_to :parent
  has_many :children
end
I can return the Area with the parent and children included in my controller:
class AreasController < ApplicationController
  ...
  def show
    area = Area.friendly.find(params[:id])
    render json: area, include: [:children, :parent]
  end
end …javascript json ruby-on-rails active-model-serializers vue.js
我正在尝试覆盖多态关系的默认序列化器。我有:
class NotificationListSerializer < ActiveModel::Serializer
  attributes :id, :title
  belongs_to :notifiable, polymorphic: true
end
如果notifiable是一个组织,则使用 OrganizationSerializer 对该组织进行序列化。如果notifiable是一个 Group,则使用 GroupSerializer 序列化该组。这是完全有道理的,但是我如何根据类指定不同的序列化器?
例如,如果notifiable是一个组织,我想使用 SparseOrganizationSerializer 而不是 OrganizationSerializer。我怎样才能实现这个目标?
我很确定这已记录在案,但我很难跟踪并找到任何示例。
从文档中:
多态关系
与任何其他关联一样,多态关系是通过指定关系来序列化的。例如:
Run Code Online (Sandbox Code Playgroud)class PictureSerializer < ActiveModel::Serializer has_one :imageable end您可以通过覆盖serializer_for来指定序列化器。有关多态关系的更多上下文,请参阅每个适配器的测试。
覆盖关联序列化器查找
如果要为关联定义特定的序列化器查找,可以重写 ActiveModel::Serializer.serializer_for 方法以根据定义的条件返回序列化器类。
Run Code Online (Sandbox Code Playgroud)class MySerializer < ActiveModel::Serializer def self.serializer_for(model, options) return SparseAdminSerializer if model.class == 'Admin' super end # the rest of the serializer end
ruby-on-rails polymorphic-associations active-model-serializers
在控制器中,我想替换if..render..else..render为respond_with:
# Current implementation (unwanted)
def create
  @product = Product.create(product_params)
  if @product.errors.empty?
    render json: @product
  else
    render json: { message: @product.errors.full_messages.to_sentence }
  end
end
# Desired implementation (wanted!)
def create
  @product = Product.create(product_params)
  respond_with(@product)
end
问题respond_with在于,如果出现验证错误,JSON 会以不符合客户端应用程序期望的特定方式呈现:
# What the client application expects:
{
  "message": "Price must be greater than 0 and name can't be blank"
}
# What respond_with delivers (unwanted):
{
  "errors": {
    "price": [
      "must be greater than 0"
    ],
    "name": …json ruby-on-rails respond-with responders active-model-serializers
我在 rails5 中使用 Active Model Serializer 0.10.7
我想知道如何在序列化程序中访问设计 current_user。
current_user 应该默认设置为范围。
根据文档
但我的代码不能正常工作...
有人知道这个吗?
class BookSerializer < ActiveModel::Serializer
  attributes :id, :title, :url, :image, :is_reviewed
  def is_reviewed
    object.reviews.pluck(:user_id).include?(current_user.id)
  end
end 
和 Book 控制器看起来像这样。
class BooksController < ApplicationController
  def index
    @books = Book.order(created_at: :desc).page(params[:page])
    respond_to do |format|
      format.html
      format.json {render json: @books, each_serializer: BookSerializer}
    end
  end
end
使用: gem 'active_model_serializers', '~> 0.10.7'
当作为关系包含时,有没有办法从序列化程序中包含/排除选项?像这样的东西:
has_many :options, only: [:id, :name]
?
我有一个序列化程序:
class ProductSerializer < BaseSerializer
  attribute :id, :name
  has_many :options
end
Option 有自己的序列化程序,其中包括其他几个属性
class OptionSerializer < BaseSerializer
  attribute :id, :name, :created_at, :updated_at
end
但是,在Product关系中呈现时,我从序列化程序中获得了所有选项。但是当列为关系的一部分时(而不是单独列出),我只想要id并被name展示。像这样:
{
  "products": [{
    "id": "704c5a2d-ef53-4cae-9d3f-132dc18c148a",
    "name": "foo",
    "options": [{
      "id": "704c5a2d-ef53-4cae-9d3f-132dc18c148a",
      "name": "bar"
    }]
...
}
如果我覆盖关系,我可以到达那里:
def options
  object.options.select(:id, :name)
end
...但是 1)这感觉很草率,2)这会产生 N+1 个问题。我也可以创建一个NestedOptionSerializer然后使用它来渲染,但这似乎有点矫枉过正。
当序列化程序作为关系包含时,是否有更简单/更清晰的包含/排除属性的方法?像这样的东西?
has_many :options, only: [:id, …