William Pramana William Pramana - 6 months ago 46
JSON Question

Rails json output carrierwave thumbnail version

I need to render out a json output from a controller and I'd like to include the thumbnail version of my images in the json.

If I render json using only

:image_url
it outputs the full image url, if I use
:image
then it will lists all the available images (including thumbnail).

How do I render out in json just the thumbnail version of the image?

Thanks,

Answer

You should use something like this.

class ArtworkSerializer < ActiveModel::Serializer
    attributes :id, :name, :image_url

    def image_url
      object.image_url(:thumbnail)
    end
  end

Then in your JSON response, you will have an attribute :image_url.

Note: You should have a version defined :thumbnail in your carrierwave uploader.

Comments