Add multiple suggestion values in select ruby on rails - ruby-on-rails

<%= 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' } %>

Related

"Undefined" placeholder on f.select in form

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.

Save name with grouped_options_for_select

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" } %>

How to change html of the select form helper in rails?

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).

Set multi select for select tag

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 } %>

using include_blank in f.select but not working

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'}) %>

Resources