Ruby-on-rails 如何将参数传递给 ActiveModel 序列化程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23347587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:34:56  来源:igfitidea点击:

How to pass parameters to ActiveModel serializer

ruby-on-railsactive-model-serializers

提问by Bruce Xinda Lin

I'm using active model serializer. I have a model event which has_many activities.

我正在使用活动模型序列化程序。我有一个模型事件,它有_许多活动。

I want to return the event with the first n activities. I think I should pass the params n to the event serializer.

我想用前 n 个活动返回该事件。我想我应该将参数 n 传递给事件序列化程序。

采纳答案by Logan Serman

Options passed in are available through the @optionshash. So if you do:

传入的选项可通过@options哈希获得。所以如果你这样做:

respond_with @event, activity_count: 5

You can use @options[:activity_count]within the serializer.

您可以@options[:activity_count]在序列化程序中使用。

回答by Eric Norcross

In version ~> 0.10.0you need to use @instance_options. Using @Jon Gold example from above:

在版本中,~> 0.10.0您需要使用@instance_options. 使用上面的@Jon Gold 示例:

# controller
def action
  render json: @model, option_name: value
end

# serializer
class ModelSerializer::ActiveModel::Serializer
  def some_method
    puts @instance_options[:option_name]
  end
end

回答by Jon Gold

The @optionshash was removed in 0.9; looks like an equivalent method was recently added-

@options散列在除去0.9; 看起来最近添加了一个等效的方法-

def action
  render json: @model, option_name: value
end

class ModelSerializer::ActiveModel::Serializer
  def some_method
    puts serialization_options[:option_name]
  end
end

回答by wintondeshong

Using 0.9.3 you can use #serialization_options like so...

使用 0.9.3 你可以像这样使用 #serialization_options ......

# app/serializers/paginated_form_serializer.rb
class PaginatedFormSerializer < ActiveModel::Serializer
  attributes :rows, :total_count

  def rows
    object.map { |o| FormSerializer.new(o) }
  end

  def total_count
    serialization_options[:total_count]
  end
end

# app/controllers/api/forms_controller.rb
class Api::FormsController < Api::ApiController
  def index
    forms = Form.page(params[:page_index]).per(params[:page_size])
    render json: forms, serializer: PaginatedFormSerializer, total_count: Form.count, status: :ok
  end
end

回答by jubeless

As of 0.10of active model serializeryou can pass arbitrary options via the instance_optionsvariable as seen here.

作为0.10主动型串行器,你可以通过传递任意选择instance_options所看到的变量在这里

# posts_controller.rb
class PostsController < ApplicationController
  def dashboard
    render json: @post, user_id: 12
  end
end

# post_serializer.rb
class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :body
  def comments_by_me
    Comments.where(user_id: instance_options[:user_id], post_id: object.id)
  end
end

回答by Pushp Raj Saurabh

serialization_optionsworks well with Active Model Serialization 0.9.3.

serialization_options适用于 Active Model Serialization 0.9.3。

The options passed along with render command can be accessed in the serializer using their keys -> serialization_options[:key]

可以在序列化程序中使用它们的键 -> serialization_options[:key]访问与渲染命令一起传递的选项

回答by Mohanraj

simple way is just add activities method in the event serializer and return n number of activities. That is it.

简单的方法是在事件序列化器中添加活动方法并返回 n 个活动。这就对了。

class EventSerializer < ActiveModel::Serializer

  has_many :activities

  def activities
    object.activities[0..9] # Select whatever you want
  end
end