标签: active-model-serializers

如何为特定控制器禁用ActiveModel :: Serializers?

我们在Rails应用程序中使用active_model_serializers - 0.8.1.

该应用程序有一些API特定的控制器继承自ActionController :: Metal,其方式类似于rails-apiActionController :: API.

好吧,我们只想将ActiveModel :: Serializers用于上面提到的API控制器.
这可能吗,怎么样?

注意:
文档中所述,通过替换可以明确避免使用序列化程序

render :json

有:

render :json => @your_object.to_json

我们正在寻求比上述解决方案更优雅的解决方案.
谢谢.

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

9
推荐指数
4
解决办法
5365
查看次数

活动模型序列化程序不使用rails-api gem

我在我的json api项目中使用rails-api gem,为此我使用了活动模型序列化程序gem来序列化我的对象,但是有些对象没有使用活动模型序列化程序进行序列化.

我的serializers文件夹里面有一个MessageSerializer

class MessageSerializer < ActiveModel::Serializer
  attributes :id, :sender_id, :recipient_id, :sender_type, :subj, :body, :status, :sender

  def sender
    object.user.try('username')
  end
end
Run Code Online (Sandbox Code Playgroud)

我的消息控制器如下

class Api::MessagesController < Api::BaseController

  def index
    @messages = current_user.incoming_messages
    render json: @messages, serializer: MessageSerializer
  end

end
Run Code Online (Sandbox Code Playgroud)

但问题是抛出到客户端的序列化对象包含消息模型中的所有字段,即; 它也包含created_at,updated_at字段.

好像它不使用序列化器.

可能出了什么问题?我搜索了很多关于它但没有发现任何帮助我的帖子.

谢谢

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

9
推荐指数
3
解决办法
2498
查看次数

在rails api活动模型序列化程序中为关联属性使用不同的键名

我正在使用rails-apiactive-model-serializer构建Rest API,以便轻松过滤JSON中的必需字段.我也在这些序列化器中使用关联.我想知道的是如何为属性指定不同的键名.has_onehas_one

也就是说,我有两个模特说:Employee而且Address,有一个has_one :address说法EmployeeSerializer.我得到的回应是:

{
  id: 1,
  address: {
   street: "some_street",
   city: "some_city"
  }
}
Run Code Online (Sandbox Code Playgroud)

但我想得到以下回应:

{
  id: 1,
  stays: {
   street: "some_street",
   city: "some_city"
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过使用has_one :address, :key => :stays,但这似乎不起作用.

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

9
推荐指数
2
解决办法
6241
查看次数

序列化日期属性

我正在使用active_model_serializers和ember.js.我的一个模型有一个日期属性.在rails中,日期属性以"YYYY-MM-DD"的格式序列化.

问题; 当ember-data使用javascript Date构造函数对日期进行反序列化时,它会假定"不正确"的时区.

*不正确不是最好的词,但它是不正确的,因为我希望它默认为当前时区.DS.Model日期属性错误地解析日期(YYYY-MM-DD)

我认为active_model_serializer应该采用date属性并将其转换为iso8601格式.

 Object.date.to_time_in_current_zone.iso8601
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉active_model_serializers如何序列化所有日期对象?或者我应该在javascript中修复时区问题?

ember.js ember-data active-model-serializers

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

活动模型序列化程序中的资产管道

我试图通过包含ActiveView :: Helpers在我的模型序列化器输出中包含一个图像资源管道URL:

class PostSerializer < ActiveModel::Serializer
  include ActiveView::Helpers

  attributes :post_image

  def post_image
    image_path "posts/#{object.id}"
  end
end
Run Code Online (Sandbox Code Playgroud)

结果是/images/posts/{id}而不是资产管道路径的有效路径,即./assets/images/posts/{id}.如何在序列化程序输出中包含有效的资产管道路径?

ruby-on-rails asset-pipeline active-model-serializers ruby-on-rails-4

8
推荐指数
2
解决办法
2049
查看次数

在 Rails 中,为什么使用 Active Model Serializers 时我的更新/PATCH/PUT 会收到“204 - 无内容”响应?

此代码用于用户列表(用户可以创建用户待办事项列表)。此特定资源不包含列表项,而仅包含列表标题和列表类型。

class Api::V1::UserListsController < ApplicationController
    respond_to :json
    skip_before_filter :verify_authenticity_token

    def index
        if authenticate_user
            user_lists = @current_user.user_lists
            if user_lists
                respond_with user_lists, each_serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not find user's lists."}, status: :not_found
            end 
        else
            render json: { error: "User is not signed in." }, status: :unauthorized
        end     
    end         

    def show
        if authenticate_user
            user_lists = @current_user.user_lists
            user_list = user_lists.find_by_id(params[:id])
            if user_list
                respond_with user_list, serializer: Api::V1::UserListSerializer
            else
                render json: { error: "Could not find user's list."}, status: :not_found
            end 
        else …
Run Code Online (Sandbox Code Playgroud)

rest ruby-on-rails put active-model-serializers

8
推荐指数
2
解决办法
7837
查看次数

如何在Active Model Serializers上动态添加属性

我想决定在我的控制器中输出的属性数量.

但我不知道必须这样做吗?

controller.rb

  respond_to do |format|
    if fields
      # less attributes : only a,c
    elsif xxx
      # default attributes
    else
      # all attributes : a,c,b,d,...
    end
  end
Run Code Online (Sandbox Code Playgroud)

serializer.rb

class WeatherLogSerializer < ActiveModel::Serializer
  attributes :id, :temperature
  def temperature
    "Celsius: #{object.air_temperature.to_f}"
  end
end
Run Code Online (Sandbox Code Playgroud)

active-model-serializers ruby-on-rails-4

8
推荐指数
2
解决办法
1万
查看次数

如何为活动模型序列化程序关系选择所需的属性

我使用JSONAPI格式Active Model Serializers来创建带有rails-api的api.

我有一个序列化程序,它显示了一个特定的post,有很多topics,目前,在关系下,列出这些主题.它目前只列出id和类型.我也想展示这个主题的标题.

有些人会说include: 'topics'在我的控制器中使用,但我不需要完整的主题记录,只需要它的标题.

问题:如何指定要从主题中显示哪些属性?

是)我有的

"data": {
  "id": "26",
  "type": "posts",
  "attributes": {
    "title": "Test Title 11"
  },
  "relationships": {
    "topics": {
      "data": [
        {
          "id": "1",
          "type": "topics"
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想要的是

"data": {
  "id": "26",
  "type": "posts",
  "attributes": {
    "title": "Test Title 11"
  },
  "relationships": {
    "topics": {
      "data": [
        {
          "id": "1",
          "type": "topics",
          "title": "Topic Title"
        }
      ] …
Run Code Online (Sandbox Code Playgroud)

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

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

Active Model Serializer,测试使用哪个序列化器来呈现响应

我正在使用活动模型序列化器来从rails控制器呈现JSON响应.

我有这样的控制器动作:

def show
  @foo = Foo.find(params[:id])
  if @foo.user == current_user
    render json: @foo, serializer: FooSerializer
  else
    render json: @foo, serializer: TrimmedFooSerializer
  end
end
Run Code Online (Sandbox Code Playgroud)

我希望能够测试在我的Rspec控制器测试中使用了哪个序列化器.是否有可能从测试中获得对序列化器的引用?

更新:

我不认为这是正确使用序列化器.我现在在序列化程序本身中有逻辑来有条件地包含属性.控制器不应该真正关心使用哪个序列化器.

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

7
推荐指数
2
解决办法
3415
查看次数

如何在Rails Active Model Serializer v0.8中有条件地包含关联

我已经将AMS(0.8)与Rails 3.2.19一起使用,但我真正为它们努力的一个地方是如何控制序列化器是否包含它们的关联.我显然使用AMS来构建JSON Api.有时,序列化程序是叶子或最远的元素,有时它是顶级的,需要包含关联.我的问题是这样做的最佳方式是什么,或者是我在下面工作的解决方案(或者是最佳解决方案).

我已经看到了一些讨论,但我发现它们非常令人困惑(并且基于版本).很明显,对于Serializer属性或关联,有一个include_XXX?每个方法,你可以在这里返回truthy或falsey语句.

这是我提出的代码 - 它是一个拥有许多wine_items的酿酒师.这是你怎么做的?

模型类:

class WineItem < ActiveRecord::Base
  attr_accessible :name, :winemaker_id
  belongs_to :winemaker

end

class Winemaker < ActiveRecord::Base
  attr_accessible :name
  has_many :wine_items
  attr_accessor :show_items
end
Run Code Online (Sandbox Code Playgroud)

串行器:

class WinemakerSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :wine_items 

  def include_wine_items?
    object.show_items
  end
end

class WineItemSerializer < ActiveModel::Serializer
  attributes :id, :name
end
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

class ApiWinemakersController < ApplicationController
  def index
    @winemakers=Winemaker.all
    @winemakers.each { |wm| wm.show_items=true }
    render json: @winemakers, each_serializer: WinemakerSerializer, root: "data"
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails active-model-serializers

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