samedi 9 avril 2022

How to get an array of values from a group of checkbox in Ruby on Rails?

I'm new to Rails and I don't understand how to solve this problem.

I've got 2 entities: Player and Call Up; N:M relationship. So basically a call up has an id, some attributes (like opponents, date...) and a list of players.

In the new call up's form i added a table of players that could be included. Each row has checkbox with the corresponding player id. Here's the view:

<%= form_with(model: call_up) do |form| %>
<!--Call Up Informations-->
<!---->
  <table class="table">
    <thead>
      <tr>
        <th colspan="5"></th>
      </tr>
    </thead>
    <tbody>
      <% @players.each do |player| %>
        <tr>
          <td><%=player.first_name %></td>
          <td><%=player.last_name %></td>
          <td><%=player.number %></td>
          <td><%=player.role %></td>
          <div class="field">
            <td><%=form.check_box :player_ids, class: "form-check-input", value: player.id %></td>
          </div>
        </tr>
      <% end %>
    </tbody>
  </table>

  <br>

  <div class="actions text-center">
    <%= form.submit "Create Call Up", class: "btn btn-dark" %>
  </div>
<% end %>

In the controller i tried to get the player ids array by using params[:player_ids] but it produce a nil error. Can anyone help me out?




Aucun commentaire:

Enregistrer un commentaire