Dynamic image_tag based on collection selection

Viewed 40

I'm using active storage and displaying images that are stored on disk like so:

 <%= image_tag Image.first.attachment, height: 200, width: 200 %>

This previews the image as I would expect but I don't want to hardcode the particular image, I want to dynamically render based on a selected Image from a dropdown:

# This adds a collection of images to a dropdown selection
<%= f.input :image_id, collection: available_images, include_blank: true %>

<h4>Image Preview</h4>
<%= image_tag Image.first.attachment, height: 150, width: 150 %>

I want to render a preview of whatever image is selected from the collection/dropdown. Is there a way with javascript that I can push the selected image onto the image_tag so that it will display the chosen image? Any help on this would be appreciated.

0 Answers
Related