Ruby on Rails -- multiple selection in f.select
Ruby on-RailsRuby on-Rails Problem Overview
I have the following select box in my form:
Related Type: <%= f.select(:TYPE, [['Type A', 'Type A'],
['Type B', 'Type B'],
['Type C', 'Type C'],
['Type D', 'Type D'],
['Type E', 'Type E']
],{ :prompt => "Please select"}
) %>
I want to allow the user to make multiple selections and also make the size of the select box 5.
How to do that for the code above?
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
After your { :prompt => "Please select"}
add another hash with html options e.g.
<%= f.select(:TYPE, [['Type A', 'Type A'],
['Type B', 'Type B'],
['Type C', 'Type C'],
['Type D', 'Type D'],
['Type E', 'Type E']
],{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }
) %>
Once you've done this you might want to move your :prompt
option (keep the empty {}
though so that html attributes don't get treated as Rails options.)
Also you'll need to ensure your controller code is correctly accepting and handling multiple values.
Solution 2 - Ruby on-Rails
In case of collection, try
<%= f.select(:TYPE, Categories.collect {|p| [ p.name, p.id ] },
{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }) %>
Solution 3 - Ruby on-Rails
I have a fully working example (including preselection when editing the object), when:
Object
is the considered objectsimilar_ids
is the key to relations, and is astring
In the form:
form_for(@object) do |f|
= f.select :similar_ids, options_from_collection_for_select(Object.all, :id, :name, {:selected => @object.similar_ids.split(';')}), {}, {:multiple => true, :size => 4, :name => 'object[similar_ids][]'}
And in the Object.rb
model:
class Object < ActiveRecord::Base
before_save :handle_similars
def handle_similars
self.similar_ids = self.similar_ids.select(&:present?).join(';')
# .select(&:present?) is necessary to avoid empty objects to be stored
end
def similars
self.class.find(self.similar_ids.split(';'))
end
end
These posts helped me out:
- https://stackoverflow.com/questions/21667251/select-tag-with-multiple-values-pre-selected-values-inserted-manually-in-datab
- https://stackoverflow.com/questions/3089849/ruby-on-rails-submitting-an-array-in-a-form
Hope it helps
Solution 4 - Ruby on-Rails
HTML
<%= form.select(:product_ids, Product.all.collect {|p| [ p.name, p.id ] },
{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }) %>
Controller
@category = Category.new(category_params)
def category_params
params.require(:category).permit(:name, product_ids: [])
end
Solution 5 - Ruby on-Rails
{ :prompt => "Please select"}, { :multiple => true, :size => 5 } {} is important when f.select
Solution 6 - Ruby on-Rails
with bootstrap selectpicker and pre selected values:
= simple_form_for [:backend, @user], html: { autocomplete: 'off' } do |f|
= f.select :role_ids, options_for_select(Role.all.map{|role| [role.name, role.id]}, @user.role_ids), {}, {:multiple => true, inlcude_blank: false, class: "form-control input-sm selectpicker"}
in controller:
def user_params
params.require(:user).permit(:id, role_ids: [])
end
# only if you havent build in new action
def new
# set user
@user.roles.any?
end
Solution 7 - Ruby on-Rails
<%= f.select :tag_ids, Tag.all.collect {|t| [t.name, t.id]}, { :prompt => "Please select"}, { :multiple => true, :size => 5 } %>