When I used the select_tag helper, everything was working perfect with:
<%= select_tag :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), { class: "select", id: "district" } %>
But I can't handle the select helper. Tried changing these ways:
1) <%= form.select :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), html_options: { class: "select_inside_col", id: "locality" } %>
2) <%= form.select :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), html: { class: "select_inside_col", id: "locality" } %>
3) <%= form.select :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), class: "select_inside_col", id: "locality" %>
The result is always the same - it ignores html options(<select name="district_id" id="district_id">), however, options_from_collection_for_select work fine.
What's the trick?
Try this as per the documentation here:
<%= form.select :district_id, districts.map { |d| [d.title, d.id] }, { include_blank: "All" }, { class: "select_inside_col", id: "locality" } %>
Note, it takes the collection of districts as 2nd argument directly (not wrapped in options_from_collection_for_select).
Related
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.
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} %>
<%= 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' } %>
i have a select tag which i am generating by looping the records and creating options manually. Here is the code:
<%= f.select (:book_id_eq_any) do %>
<%= content_tag(:option, "Choose your option", value: "", disabled: '', selected: '') %>
<% #books.each do |book| %>
<%= content_tag(:option, book.book_title, value: book.id) %>
<% end %>
<% end %>
When i add {multiple: true}, it does not work. Can anyone give me hint about it. Thanks
Just use the built-in select form helper:
<%= f.collection_select :book_id_eq_any, #books, :id, :book_title, { include_blank: "Choose your option" }, { multiple: true } %>