Firstly, I need to implement this HTML form in Rails, its a dropdown of languages that can be multi-selected. Secondly, what is the best approach to store the multiple languages selected by the user?
<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>
First to store the multiple values of the Users Languages, create a Language model that has a one-to-many relationship with the User model.
class Language < ActiveRecord::Base belongs_to :user end
To store the options of the Languages you can create an enum in the Users model:
class User < ActiveRecord::Base has_many :languages enum language: {:english=>1, :arabic=>2, :french=>3, :spanish=>4} end
And to add the multiselect in the form try (also a nice alternative is using check_box):
<%= f.select :languages, {}, User.languages.each { |k, v| [k, v]}, :multiple => true %>
\n\n
<% User.languages.each do |k, v| %> <%= check_box :languages, k.humanize, value: v %> <%= label :languages, k.humanize %> <% end %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.