I have a Rails form which includes a dropdown as shown below:
<%= f.select :facility_id, options_for_select(#entities.collect{ |e| [e.entity, e.entity] }, 1 ), {include_blank: '-Select-'}, { id: my_entity'} %>
I also have 2 more dropdowns similar to the one above.
I would like to make the above dropdown as a multiselect dropdown. How can I achieve that?
Please help!
Thanks
You may just need to add this to the tag
{ :multiple => true}
like this
<%= f.select :facility_id, options_for_select(#entities.collect{ |e| [e.entity, e.entity] }, 1 ), {prompt: '-Select-', :multiple => true}, { id: my_entity'} %>
You can do it by adding multiple: true in select
Example:
<%= f.select :facility_id, options_for_select(#entities.collect{ |e| [e.entity, e.entity] }, 1 ), {include_blank: '-Select-'}, { id: my_entity'}, { :multiple => true, :size => 5 } %>
Reference: [1][2]
Related
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?
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.
In case of collection, try
<%= f.select(:TYPE, Categories.collect {|p| [ p.name, p.id ] },
{ :prompt => "Please select"},
{ :multiple => true, :size => 5 }) %>
I have a fully working example (including preselection when editing the object), when:
Object is the considered object
similar_ids is the key to relations, and is a string
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:
Select tag with multiple values pre-selected - Values inserted manually in database
Ruby on Rails: Submitting an array in a form
Hope it helps
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
{ :prompt => "Please select"}, { :multiple => true, :size => 5 } {} is important when f.select
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
Multiple select:
= form_with url: ui_dashboard_diagrams_path, method: :get, local: true do |f|
.row
.col.form-group
= f.select :our_organization_ids,
options_for_select(OurOrganization.pluck(:name, :id), params[:our_organization_ids]),
{ include_blank: '' },
{ multiple: true, class: 'form-control form-select-multiple' }
javascript:
$(document).ready(function() {
$('.form-select-multiple').select2({
allowClear: true,
multiple: true
});
});
Add permit array our_organization_ids to your controller:
private
def diagrams_params
params.permit([our_organization_ids: []])
end
<%= f.select :tag_ids, Tag.all.collect {|t| [t.name, t.id]}, { :prompt => "Please select"}, { :multiple => true, :size => 5 } %>
I have a problem with grouped_options_for_select.
My categories are correctly add in my database but the names are not save when i return on my form.
<% categories = {
'Finances' => [['Note de frais', 3], ['Devis', 5]],
'Marketing' => [['Mailing', 4], ['Réseaux Sociaux',6]]
} %>
<%= f.select :category_ids, grouped_options_for_select(categories), {}, { multiple: true, class: "selectize" } %>
I would like to have the same result as this :
<%= f.select :category_ids, Category.all.pluck(:name, :id), {}, {multiple: true, class:"selectize"}%>
Thanks for your help !
for the grouped_options_for_select you can pass a parameter for selected_key, so you can specify the one that is selected when you load the view, so you just have to pass the value there
<%= f.select :category_ids, grouped_options_for_select(categories, selected_key: #record.category_id), {}, { multiple: true, class: "selectize" } %>
This is my select tag that I would change for specific values:
<div class="select_field">
<%= select_tag
"quote[service_id]",options_for_select(AvailableService.all.map { |u|
[u.service_type.titleize+" - "+ (u.service_name == "housekeeping" ?
"House cleaning" : u.service_name), u.id] }), { prompt: "Select
Service", required: true } %>
</div>
You can use collection_select
<%= collection_select :service_id, AvailableService.where(service_type:"cleaning"),
:id, :service_name, {}, {prompt:"Select Service", required: true} %>
I want to use dropdown to list all my projects and the first option is blank.
but when I add :include_blank => true, it doesn't work.
just like that:
<%= f.select(:project_id, :include_blank => true) do %>
<% #projects.each do |p| %>
<%= content_tag(:option, p.name, value: p.id) %>
<% end -%>
<% end -%>
but if I use
<%= f.select(:title, %w{ Male Female }, :include_blank => true) %>
It works exactly!
I don't know how to revise the first code to make the blank appear.
Thanks!
Use like this:
<%= f.select :category_id, nil, :include_blank => true %>
In the documention API LINK
select(object, method, choices = nil, options = {}, html_options = {}, &block)
I think the problem is the <%= content_tag … %>. Try this:
<%= f.select(:project_id, :include_blank => true) do
#projects.each do |p|
content_tag(:option, p.name, value: p.id)
end
end %>
(ie. just a single ERB output embedding tag)
Update: looking over this again, you are not really customising the way the options are generated, do you really need the block?
<%= f.select(:project_id, #projects.collect { |p| [p.name, p.id] }, :include_blank => true) %>
U can do this:
<%= f.select(:project, :id, #projects.collect {|p| [ p.name, p.id ] }, {:include_blank => true}) %>
Or
<%= f.select(:project_id, #projects.collect {|p| [ p.name, p.id ] }, {:include_blank => true}) %>
Or you can use prompt:
<%= f.select(:project, :id, #projects.collect {|p| [ p.name, p.id ] }, {:prompt => 'select project'}) %>
I'm using the gem Ransack in a Rails 3.2 app.
The following works to select multiple values:
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, {}, { :multiple => true}
But, is there a way to include a blank?
I've tried these:
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, {}, { :multiple => true, :include_blank => true }%></td>
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, include_blank: true, {}, { :multiple => true }%></td>
Thanks for the help!!
OK - this worked:
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, {:include_blank => true}, { :multiple => true }%></td>