Rails Multiple Check Box with drop down list

2019-09-12 23:09发布

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条回答
Evening l夕情丶
2楼-- · 2019-09-12 23:50

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

查看更多
登录 后发表回答