首先,我需要在 Rails 中实现这个 HTML 表单,它是一个可以多选的语言下拉列表。其次,存储用户选择的多种语言的最佳方法是什么?
<div class="button-group">
<label class="form-control" data-toggle="dropdown">Language <span class="fa fa-angle-down"></span> </label>
<ul class="dropdown-menu2 dropdown-menu">
<li>
<a href="#" class="small" data-value="" tabIndex="-1"><input type="checkbox" /> No Other Language</a>
</li>
<li>
<a href="#" class="small" data-value="arabic" tabIndex="-1"><input type="checkbox" /> Arabic</a>
</li>
<li>
<a href="#" class="small" data-value="french" tabIndex="-1"><input type="checkbox" /> French</a>
</li>
<li>
<a href="#" class="small" data-value="spanish" tabIndex="-1"><input type="checkbox" /> Spanish</a>
</li>
</ul>
</div>
最佳答案
class Language < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :languages
enum language: {:english=>1, :arabic=>2, :french=>3, :spanish=>4}
end
<%= f.select :languages, {}, User.languages.each { |k, v| [k, v]}, :multiple => true %>
<% User.languages.each do |k, v| %>
<%= check_box :languages, k.humanize, value: v %>
<%= label :languages, k.humanize %>
<% end %>
关于html - Ruby on Rails 多选下拉菜单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49569560/