I have this loop in my view:
<% @item.inventory_items.each do |p| %>
I have this form that is looped in the above do
block for each inventory_item
:
<%= form_for(@list_item) do |f| %>
<%= f.hidden_field :upc, :value => @item.upc %>
<%= f.hidden_field :inventory_item_id, :value => p.id %>
<%= f.select :shopping_list_id, options_for_select(ShoppingList.options_for_list(current_user.shopping_lists)), {}, :class => "form-control" %>
<%= f.submit("Add It!", class: "btn btn-add") %>
<% end %>
I've now put this form into a Bootstrap 3 modal. For brevity's sake I won't post the whole modal. Here's the trigger, which is looped for each inventory_item
:
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#formModal">Add To List</button>
The problem I'm running into, is that my form uses attributes from the looped inventory_items
to populate one of the hidden fields in this form. So here's my question:
How can I pass this variable from the loop to a modal when it is triggered?
EDIT
Here's my current modal:
<div class="modal fade" id="addListItem" tableindex="-1" role="dialog" aria-labelledby="addListItem" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="form-label">Add this item to a shopping list.</h4>
</div>
<div class="modal-body">
<%= form_for(@list_item) do |f| %>
<%= f.hidden_field :upc, :value => @item.upc %>
<%= f.hidden_field :inventory_item_id, :id => "inventory-item-id" %>
<%= f.select :shopping_list_id, options_for_select(ShoppingList.options_for_list(current_user.shopping_lists)), {}, :class => "form-control" %>
<%= f.submit("Add It!", class: "btn btn-add") %>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Custom .js from @David Antaramian:
$('button,[data-inventory-item-id]').click(function() {
var inventoryID = $(this).data('inventory-item-id');
$('#inventoryID').prop('value', inventoryID);
$('#addListItem').modal('show');
});