Active Model Serializer和Paperclip尺寸不同

tim*_*one 3 ruby-on-rails paperclip ruby-on-rails-3.2 active-model-serializers

我有一个资产模型类,它使用paperclip 3.5.2有不同的大小:

class AssetSerializer < ActiveModel::Serializer
  attributes :id, :asset # works fine
  # would like to output small but don't seem to be able to
  #attributes :id, :asset, :asset(:small) 
end
Run Code Online (Sandbox Code Playgroud)

这有点令人困惑,因为Paperclip使用了名称类,而模型被称为类(确实令人困惑).我收到以下错误:

/Users/jt/repos/rails/app/serializers/asset_serializer.rb:2: syntax error, unexpected '(', expecting keyword_end
  attributes :id, :asset, :asset(:small)
Run Code Online (Sandbox Code Playgroud)

它显然不喜欢传递给资产的论点

小智 6

您只需在序列化程序中添加自定义属性即可

他们在文档中有一个示例https://github.com/rails-api/active_model_serializers#attributes

以下是您在示例中使用的内容.

class AssetSerializer < ActiveModel::Serializer
  attributes :id, :asset, :asset_small

  def asset_small
    object.asset.url(:small)
  end
end
Run Code Online (Sandbox Code Playgroud)