Rails Multiple Check Box with drop down list

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!