Reputation: 25
I have an app where I'd like to allow users to add a movie to either one or multiple lists when adding it to a DB. Right now, I'm using a radio button and it only lets the user choose one list at a time. Can someone explain how this would work using a collection_select or something similar?
My form:
<div class="modal fade" id="movielistmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="movielistmodal">Add Movie To List</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<%= form_for @movie, method: :post, url: 'add_api_movie_to_list' do |f| %>
<%= hidden_field_tag :movie_id, nil, { id: "movie-id" } %>
<% @list.each do |list| %>
<%= f.label :name, list.name %>
<%= f.radio_button :list_ids, list.id %>
<% end %>
<%= f.label :rating %>
<%= f.number_field :rating, min: 0, max: 5 %>
<%= f.submit "Add Movie", class: 'btn btn-primary btn-space btn-sm' %>
<% end %>
</div>
</div>
</div>
</div>
Upvotes: 0
Views: 52
Reputation: 2321
You can add collection_check_boxes
<%= form_for @movie, method: :post, url: 'add_api_movie_to_list' do |f| %>
<%= f.collection_check_boxes :list_ids, List.id(:name), :id, :name %>
<%= f.submit "Add Movie", class: 'btn btn-primary btn-space btn-sm' %>
<% end %>
This is in the view
<% List.all.each do |list| %>
<%= link_to list.name,cars_path(params.merge(list_id: list.id)), class: "#{'shadow' if params[:list_id].to_i == list.id}" %><br>
<% end %><hr>
Let me know if this does work for you
Upvotes: 1