I would like to capture participant names in a web form. On page load the quantity field is set to 1 by default and one name entry field renders. When a user changes the number in the name entry field, I would like the new number to be passed to the controller action to render n.times
name entry fields, where n corresponds to the number in the quantity field.
Coffeescript
ready = ->
$('#quantity-select').change ->
qty = document.getElementById('quantity-select').value
$.ajax
complete: (request) ->
data: { quantity: qty }
console.log(qty)
return
return
$(document).ready(ready)
Controller
def show
@item = Item.find(params[:id])
@order = Order.new
@order_item = @order.order_items.build
if params[:quantity].present?
@n = params[:quantity]
puts "received #{@n}"
else
@n = 1
puts "not received"
end
@n.to_i.times {@order_item.participants.build}
end
By using puts
in the controller action I can confirm that the controller receives the value from the quantity field. The problem is that when the value of @n
changes the site does not refresh and no additional name entry fields appear. How do I get the site to reload the with the name entry field?