I'm trying to create an individual record for each name in an array. The array originates via a form in one controller and the individual records need to be saved in a join (type) table (I know there are not "join" tables in mongo, but it's the best way to describe it). Currently running Rails 5 with Mongoid/MongoDB.
Originating Form:
<%= form_tag create_multiple_batch_keg_index_path, method:
:create do |form|%>
<div class="field">
<table class="table table-hover table-condensed">
<thead>
<tr>
<th>Select All</th>
<th>Keg Name</th>
</tr>
</thead>
<tbody>
<% Keg.each do |batch_keg| %>
<tr>
<td><%= check_box_tag 'batch_keg_ids[]', batch_keg.id
-%> </td>
<td><%= batch_keg.name -%> </td>
</tr>
<% end %>
</tbody>
</table>
</div>
Originating Controller Params:
def batch_params
params.require(:batch).permit(:batch_keg_attributes => [keg_id,
:active, :visible, :wholesale_inventory, :taproom_inventory,
:hold_inventory])
end
Join Controller
def create_multiple
batch_keg_ids(params).flatted.map{|ids|
BatchKeg.create(:wholesale_inventory => true, :taproom_inventory
=> true, :hold_inventory => false, :active => true, :visible =>
true)}redirect_to batches_url
end
Routes
resources :batch_keg do
collection do
post :create_multiple
put :update_multiple
get :collection
end
end
I think I've got most of the process completed successfully (I've worked through several error messages to this point, but I'm stuck). I've searched all over the inter-webs trying to find a solution but haven't been able to find one that has worked. I'm either A), close but not quite there, or B) completely off course.
It looks like I'm getting the data I need through, but unclear on how I need to use it in the join controller (I think), I've been on this for hours and my brain is mush. I feel like I need to create a variable in the join controller to hold the data, which is here I was headed. Thanks in advance for any suggestions for this code, or a more efficient way to do this.
BatchKegfor eachKegyou select in the form? Looks like you are doing that in the "join" controller, but you are not saving thekeg_idin theBatchKegcreate call. I'm not really sure what the problem is. Please try to clean up the code you posted as there seems to be some typos.