我有一个应用程序,允许用户在将电影添加到数据库时将其添加到一个或多个列表中。现在,我正在使用单选按钮,它只允许用户一次选择一个列表。有人可以使用collection_select或类似的东西解释它如何工作吗?
我的表单:
<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>
答案 0 :(得分:1)
您可以添加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 %>
这是在视图中
<% 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>
让我知道这是否对您有用