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" } %>
Related
I know there are some other questions like this, but they didn't work for me. I appreciate any contribution or help.
<%= f.select(:status) do %>
<% [['Activo', 1], ['Inactivo', 2]].each do |c| -%>
<%= content_tag(:option, c.first, value: c.last) %>
<% end %>
<% end %>
You can use options_for_select to add the options and use third argument to add html options
<%= f.select :status, options_for_select([['Activo', 1], ['Inactivo', 2]]), {}, { :class => 'form-control' } %>
select takes two options hashes, the second for html options.
To build on your example code in a single (long) line:
= f.select :status, [['Activo', 1], ['Inactivo', 2]].each { |c| content_tag(:option, c.first, value: c.last) }, {}, class: "form-control"
This produces the same HTML as:
= f.select :status, [['Activo', 1], ['Inactivo', 2]], {}, class: "form-control"
select docs
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]
I have a form
<%= form_for #user, url: contact_path do |form| %>
<%= form.select(:email, User.all.map(&:email), {}, { class: 'my-form' }) %>
<% end %>
which works well but has placeholder "Undefined" in start position.
I tried to get rid of that with
<%= form.select(:email, User.all.map(&:email), {placeholder: "Select email"}, { class: 'my-form' }) %>
or
<%= form.select(:email, User.all.map(&:email), {prompt: "Select email"}, { class: 'my-form' }) %>
but still same. Any ideas?
<%= form.select :email, options_for_select(User.all.map(&:email)), include_blank: "whatever your prompt says" , class: 'my-form' %>
Experienced same issues today and this is what I did for one of our projects recently.
<%= f.select :category, options_for_select(Category.all.collect { |c| [c.name, c.id] }), { include_blank: 'Select category' }, { class: 'custom-select' } %>
Try changing the values according to your values.
This is how you add class and placeholder in a form.select element in Rails.
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} %>
<%= f.select :id, lists.collect{ |p| [p.name, p.id] } << "add product", { prompt: t("select product") }, { :class => "selectpicker" } %>
The problem is that "add product" is being added on the last index but I want it on the second index.
You can use insert and pass the index you want to insert the data in:
Tweaking a bit the code:
<%= f.select :id, lists.map { |p| [p.name, p.id] }.insert(1, 'add product'), { prompt: t('select product') }, { class: 'selectpicker' } %>