William Pramana William Pramana - 2 years ago 188
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

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

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


Answer Source

You should use something like this.

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

    def image_url

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download