5

Long time lurker but haven't posted yet. Despite hours of debugging I can't seem to figure out why my check_box_tag won't pass an array. Essentially I would like the check box behaviour to allow the user to select multiple records and then use a button to either discontinue or refill the record.

View:

<% @medications.each do |medication| %>     
  <div class="small-4 columns three-column">    
    <%= link_to medication.drug_name, edit_medication_path(medication), remote: true  %>            
  </div>
  <div class="small-3 columns three-column">
    <%= link_to medication.duration, edit_medication_path(medication), remote: true %>
  </div>
  <div class="small-3 columns three-column">
    <%= link_to medication.duration_unit, edit_medication_path(medication), remote: true %>
  </div>
  <div class="small-2 columns three-column"> 
    <%= form_tag(medication_select_path, method: 'put', :id => 'medication-select') do %>
      <%= check_box_tag "medications[]", medication.id  -%>
    <% end %>
  </div>
<% end %>

<div class="small-6 columns">
  <%= submit_tag "Discontinue", :type => 'submit', :class => 'hollow alert button', :form => 'medication-select', :value => 'Discontinue', method: 'put' %>
</div>
<div class="small-6 columns">
  <%= submit_tag "Refill", :type => 'submit', :class => 'hollow create button', :form => 'medication-select', :value => 'Refill', method: 'put' %>
</div>

Controller:

@selected_meds = Medication.find(params[:medications])

When I look at terminal after pressing the submit button I get an output with only the first medication.id listed in params despite selecting 4 records.

Terminal:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"aNWTazi45CLxMA", "medications"=>["44"], "commit"=>"Discontinue"}

Also my output HTML appears to be correct when viewing source:

<form id="medication-select" action="/medication_select" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="_method" value="put" /><input type="hidden" name="authenticity_token" value="DpJ8" />
<input type="checkbox" name="medication_id[]" id="medication_id_" value="44" />

I've tried multiple sources (and copied the code as clearly as possible) but still have been unable to get an array coming up in params ids.

http://railscasts.com/episodes/165-edit-multiple?view=asciicast http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag

As expected from the terminal output I'm getting the expected changes to medication with id=44 but if I check medication with ids of 44, 46, 50 it only performs the controller action on the first checked id.

The only thing I can think of is that maybe splitting up my submit buttons and my form_tag (but then referencing them with the html5 form tag) is changing the behaviour somehow? Thanks in advance, I've been pulling out my hair on this one.

EDIT: Noted that when I select a single check box that is not the first box listed (for example, if the second check box has a id of 46) no id is passed into params. So I think this narrows down the issue to the check_box_tag or my form_tag, however, I still can't find why this isn't working, especially as the source code all appears correct in the browser).

2 Answers 2

6

I guess the problem is with the way you have nested your form elements. For each medication, you're creating a form which implies you have n forms for n medications.

Take the form outside the loop like this:

<%= form_tag(medication_select_path, method: 'put', :id => 'medication-select') do %>
  <% @medications.each do |medication| %>
    <%= check_box_tag "medications[]", medication.id  -%>
  <% end %>
<% end %>

This way, you have a single form which when submitted will make params[:medications] contains the ids that you selected.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Arun! After a bit more poking around I was able to get it working. That's a better explanation than mine, I appreciate all the help!
1

For those who run into a similar issue I think I figured it out! Posting in case it will help out other new folk like me!

After editing the form_tag and end placements it appeared to be working as expected. From what I gather the <% end %> tag was closing the form_tag loop tag so only the first check box was associated with the form_tag.

All I needed to do was wrap everything in the form_tag.

Here's the fixed code, hopefully it will help someone else in the future!

<%= form_tag(selected_medications_path, method: 'put', :id => 'medication-select') do %>
    <% @medications.each do |medication| %>     
        <div class="small-4 columns three-column">  
            <%= link_to medication.drug_name, edit_medication_path(medication), remote: true  %>            
        </div>
        <div class="small-3 columns three-column">
            <%= link_to medication.duration, edit_medication_path(medication), remote: true %>
        </div>
        <div class="small-3 columns three-column">
            <%= link_to medication.duration_unit, edit_medication_path(medication), remote: true %>
        </div>
        <div class="small-2 columns three-column"> 
            <%= check_box_tag "medications[]", medication.id  %>
        </div>
    <% end %>
<% end %>

Comments

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.