0

enter image description hereI'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.

2
  • Ok, so you are trying to create a BatchKeg for each Keg you select in the form? Looks like you are doing that in the "join" controller, but you are not saving the keg_id in the BatchKeg create 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. Commented Sep 14, 2017 at 2:15
  • Thanks for replying Ryan! You are correct. I'm trying to save each keg_id in BatchKeg. I do need the user ID, but had a hard time getting it though, so I removed it to try and tried to get the line open and work it in from there. I thought I added a screen shot of the error message to the initial post, I just added it for reference. Commented Sep 14, 2017 at 2:48

1 Answer 1

1

Well the error message is pretty clear. You are passing params as a parameter into batch_kegs_ids, but that method is expecting no parameters. Besides, it looks like the batch_kegs_ids method is empty (no code). I'd remove that entirely (probably not supposed to be there anyways).

I'd also change the create_multiple method to

 def create_multiple
   params[:batch_keg_ids].each do |id| 
     BatchKeg.create(keg_id: id, wholesale_inventory: true, taproom_inventory: true, hold_inventory: false, active: true, visible: true)
   end
   redirect_to batches_url
 end
Sign up to request clarification or add additional context in comments.

1 Comment

you are a lifesaver sir. Thank you. And sorry about those typos, I'm brand new to programming and this site and somehow pasted the tag for the error message image into the code in my post, I appreciate your patience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.