In my Rails 4 app I'm trying to change three text fields on an edit form when a select dropdown option has changed. Using CoffeeScript 1.7.1/Rails 4.0.2/Ruby 2.1.0.
My Coffeescript file:
$(document).ready ->
new_item = (items_array, item) ->
for si of items_array
if items_array[si][3] is item
new_studio_item = items_array[si]
return new_studio_item
$('#pk_item_studio_item_id').change ->
old_item = $('#pk_item_starting_item').val()
studio_item = $('#pk_item_studio_item_id').val()
unless old_item == studio_item
studio_items = $('#pk_item_studio_items').val()
[name, description, price, id] = new_item studio_items, studio_item
document.getElementById("pk_item_custom_name").value = name
document.getElementById("pk_item_custom_description").value = description
document.getElementById("pk_item_lab_price").value = price
return
Nothing is being assigned from the 'new_item' function on this line:
[name, description, price, id] = new_item studio_items, studio_item
Yet in coffee's REPL, this line does return values for name, description, price and id. I've tried Firefox, Chrome and Safari, and get the same results: the text field values remain unchanged.
Hopefully there's something obviously wrong with my code. Any ideas what I need to change? I'll gladly provide more code details if necessary (view/controller/etc.), just didn't want to turn this into a TLDR unnecessarily.
Update: This code returns my array to the form in a test field:
ready = ->
$('#pk_item_studio_item_id').change ->
old_item = $('#pk_item_starting_item').val()
studio_item = $('#pk_item_studio_item_id').val()
unless old_item == studio_item
studio_items = $('#pk_item_studio_items').val()
document.getElementById("test_input").value = studio_items
return
$(document).ready(ready)
$(document).on('page:load', ready)
This is the array returned:
[["Custom Name for Item 0", "Custom description of item 0", "1.1", 1], ["Custom Name for Item 1", "Custom description of item 1", "1.1", 2], ... (etc., etc.) ..., ["Custom Name for Item 39", "Custom description of item 39", "1.1", 40]]
But when the new_item function (see my first code block above) is coded to return items_array by changing the last line to return items_array, I get ,,,,,,,,,,,,,,, (etc.) returned instead of the same array as above. So I'm doing something wrong in either the way I'm sending the array to the function, or how the function is handling the array.