margo
margo

Reputation: 2927

how to add a data attribute to a formtastic form input radio collection

In a formtastic form for new products, I am displaying a collection of options for radio buttons which appear as images. I need to pass a data attribute to be used client side.

A product has many images, hence product.product_images is an array of active records. I've tried variations on the code below without success.

<%= f.input :selected_image_url, as: :radio,
  collection: (Hash[product.product_images.map { |image| [image_tag(image.thumb), image.image_url], html: {data: {image_huge: image.huge}} }]) %>

the error message is unexpected ',', expecting '}'

The code below gives the error message invalid number of elements (3 for 1..2)

<%= f.input :selected_image_url, as: :radio,
  collection: (Hash[product.product_images.map { |image| [image_tag(image.thumb), image.image_url, {"data-image-huge" => image.huge}] }]) %>

Any ideas?

Upvotes: 1

Views: 1142

Answers (1)

margo
margo

Reputation: 2927

Here's what worked

<%= f.input :selected_image_url, as: :radio, 
  collection: product.product_images.map { |image| [image_tag(image.thumb), image.image_url, {'data-image-huge' => image.huge}] } %>

Upvotes: 4

Related Questions