问题
I have created an associated models to create a check box list, where user can select languages that he/she speaks. I have tried to use both Chosen and Select2 but could not make it work. The problem might be because I use check_box_tag
Here is my code;
<%= hidden_field_tag "user[language_ids][]", nil %>
<% Language.all.each do |language| %>
<%= check_box_tag "user[language_ids][]", language.id, @user.language_ids.include?(language.id), id: dom_id(language) %>
<%= label_tag dom_id(language), language.name %> </br>
<% end %>
I have watched the Railscast Habtm video to create this. This just works fine but I would like to make it user friendly. Thank you
回答1:
You need to render a <select>
tag instead of checkboxes:
form_for @user do |f|
f.select_tag "language_ids", options_from_collection_for_select(Language.all, "id", "name")
then you can call $(..).select2()
on that DOM element
来源:https://stackoverflow.com/questions/32290105/rails-multiple-check-box-with-drop-down-list