DhatchXIX
DhatchXIX

Reputation: 437

send data link from modal to form with javascript

my error is: Uncaught TypeError: Cannot read property 'forEach' of undefined

This a mix of rails 4 and javascript. I have a button in the modal that I need to send data to a collection in the form.

The user will select a product in the modal and in the form I need a collection of the colors available for that product.

product has many shirt colors.

modal

<td><%= link_to 'Add product', '#', class: 'btn btn-success btn-add-product', data: {product: product, sizes: product.sizes, shirt_colors: product.shirt_colors} %></td>

javascript

$(".btn-add-product").click(function(){
  var product = $(this).data("product");
  var shirt_colors = $(this).data("shirt_colors");

  shirt_colors.forEach(function(shirt_color){
    $('select#color-select')
      .append('<option value="'+shirt_color.name+'">'+shirt_color.name.toUpperCase()+'</option>')
})

})

form

= f.input :color_name, label: false, collection: [], input_html: {id: 'color-select'}

Upvotes: 0

Views: 44

Answers (1)

mnort9
mnort9

Reputation: 1820

You need to grab the "data" attribute with a hyphen instead of an underscore

$(this).data("shirt-colors");

Upvotes: 1

Related Questions