Search code examples
ruby-on-railsruby-on-rails-5actionview

How do I use check_box in a table form so that I can access the table attributes in an array?


I have a table inside of a form, with the following code:

<tbody>
        <% @appointments.each do |appointment| %>

            <td class="collapsing">
                <div class="ui fitted slider checkbox">
                <%= f.check_box appointment.id %><label></label>
                </div>
            </td>
            <td><%= appointment.name %></td>

        <% end %>
</tbody>

So a checkbox is generated, but if I'm selecting multiple checkboxes, I get

Parameters: {"utf8"=>"✓", "authenticity_token"=>"qCo02PG1F2wrSK4sxCQiQLzuhG4vZypgd9p5LzP9Sp7uZQFHs8tTTitLR++VXVK3f68P0qih+iQBlZt9anG01Q==", "cleaner"=>{"4"=>"1", "5"=>"0", "2"=>"0", "3"=>"0", "6"=>"0"}, "commit"=>"Request", "cleaner_id"=>"1"}

So to access the appointments, I would do parameters["cleaner"] and get <ActionController::Parameters {"4"=>"1", "5"=>"1", "2"=>"0", "3"=>"0", "6"=>"0"} permitted: false>

What I am looking to do is get an array instead of a hash.


Solution

  • The Hash class provides a keys method, which will return the keys of a hash as an array. So parameters["cleaner"].keys is what you are (probably) looking for.