使用RABL从Paperclip返回图像URL

时间:2012-12-01 13:01:30

标签: mongoid paperclip rabl

我有一个用于头像的Paperclip的用户模型,我需要能够使用RABL为每个尺寸(小,中,大)返回image_url

在mongoid模型中,我只会做self.avatar(:original)但现在没有任何作用,我只是在附件中得到一个空的回复

"user" : {
  "id" : "50b204e10eae9c55fa000028",
  "paperclip::attachment" : {},
  "name" : "My Name"
}

/models/user.rb

has_mongoid_attached_file :avatar,
    :styles => {
      :original => ['1000x1000>', :jpg],
      :small    => ['64x64#',           :jpg],
      :medium   => ['250x250',    :jpg],
      :large    => ['500x500>',   :jpg]
    }

/views/posts/base.json.rabl

child :user do
    attributes :id, :name

    child :avatar do
        attributes :original
    end
end

1 个答案:

答案 0 :(得分:2)

试试这个:

child :user do
  attributes :id, :name

  node :avatar_original do |u|
    u.avatar(:original)
  end
end