TrolliOlli
TrolliOlli

Reputation: 915

Rails: Select items via checkboxes and then do something with them when button is pressed?

My end goal is to present a list of various active record objects, let users select some of these via checkboxes, and then finally press a button to trigger some action to be taken on them.

As an example, let's say these active record objects represent songs, and I'd like a user to be able to select a list of songs and then click a "Save Playlist" button to pass the list of selected songs to some function in my controller.

I believe that I would first start with a list of songs in my view that have checkboxes next to them. I think it would look something like this:

<table class='table table-bordered'>
  <thead>
    <tr>
      <th style="width:30px;"> Save to Playlist? </th>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <% @songs.each do |song| %>
      <tr>
        <td><%= check_box( "songs_selected", "selected_songs[]", { multiple: true }, song.id, nil) %>
        <td><%= song.name %></td>
      </tr>
    <% end %>
  </tbody>
</table>

If I'm understanding the check_box code correctly, what I have should allow me to pass a selected_songs array with the ID's of songs I have selected.

I'm just not positive how to finish this off with a button that's linked to the checkboxes so that when it's pressed it will actually pass that array to a method in my controller.

Any input is appreciated, even if it's pointing out that my design is somehow flawed and there's some better way to do it. Thanks!

Upvotes: 0

Views: 3335

Answers (2)

Anand
Anand

Reputation: 6531

The easiest way of doing this is to set those checkboxes up to become an array. and then wrap it into a from so that when button is pressed submit this form and you will get all songs_ids to your controller for example: -

<%= form_tag you_path, method: :post, id: 'send-songs-id', class: 'form-horizontal' do  %>
    <table class='table table-bordered'>
      <thead>
        <tr>
          <th style="width:30px;"> Save to Playlist? </th>
          <th>Name</th>
        </tr>
      </thead>    
      <tbody>
        <% @songs.each do |song| %>
          <tr>
              <td><%=check_box_tag 'selected_songs[]', song.id%></td>
              <td><%= song.name %></td>
          </tr>
        <% end %>
        <%= content_tag :button, :type => :submit, class: 'btn btn-success' do %>
          Submit
        <%end%>
      </tbody>
    </table>
<%end%>

this will generate html like this: -

  <input type="checkbox" name="selected_songs[]" value="1" />
  <input type="checkbox" name="selected_songs[]" value="2" />
  <input type="checkbox" name="selected_songs[]" value="3" />
  <input type="checkbox" name="selected_songs[]" value="4" />
  // and so on

in controller you can get this:

def your_action
 song_ids = params[:selected_songs]
 #......
end

Upvotes: 2

Daniel Westendorf
Daniel Westendorf

Reputation: 3465

You're on the right track.

The best way to do this would be to put it all within a form_tag (assuming pre-Rails-5.1, otherwise form_with).

Effectively, you'd have view markup like this:

<%= form_tag playlists_path %>
 <table>
    ...
    <% @songs.each do |song| %>
      <tr>
        <td><%= check_box( "songs_selected", "selected_songs[]", { multiple: true }, song.id, nil) %>
        <td><%= song.name %></td>
      </tr>
    <% end %>
  <%= submit_tag "Create Playlist" %>
<% end %>

You'll need the route for the controller method here, I've arbitrarly made up playlists_path, assuming you'd have resources :playlists, only: [:create] in your routes.rb to match your example.

Upvotes: 1

Related Questions