Reputation:
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,
Upvotes: 2
Views: 1737
Reputation: 1811
i suggest you to use Serializer like this:
class YourSerializer < ActiveModel::Serializer
attributes(
:id,
:image,
:small,
:medium,
:large
)
def small
url(width: 116, height: 116, crop: "fill")
end
def medium
url(width: 640, height: 400, crop: "fill")
end
def large
url(width: 1000, crop: "fill")
end
private
def url(options={})
options[:format] = "png"
options[:secure] = true
object.image_url(options)
end
end
if you goes to stick with thumbnail or already defined version just use:
def url
object.image_url(:thumbnail)
end
Upvotes: 2
Reputation: 1074
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.
Upvotes: 5