The answers to how do I use a default placeholder ... in a select_tag? show how to set a default value for the select field.
For example:
<%= f.select :country, countries, {include_blank: true, prompt: "United States"}, {required: true, class: "form-control"} %>
The problem here is that I only want the silhouette of the placeholder, not an actual value that could accidentally be submitted.
If we compare to the placeholder value in a text_field, the difference is much more clear. We see the placeholder:
But we cannot accidentally submit the placeholder.
How can I get similar behaviour with the .select field?
That is, so that there is some placeholder in the select field, but so it cannot be accidentally submitted?
Just change the boolean value of include_blank: true to the desired placeholder string:
<%= f.select :country,
countries,
{ include_blank: "- nothing selected -" },
{ required: true, class: "form-control" }
%>
This will create a pre-selected select option with no value, which will be interpreted by Rails controllers as nil.
According to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select a select has no placeholder option. If you wish to show a predefined value that should not be submitted you can add a disabled option to this specific item and combine it with selected.
<%= f.select :country, [['USA', 1], ['Canada', 2]], { disabled: '1', selected: '1' } %>
Related
Suppose I have this array variable in my controller: #estudiantes_seleccionados = #clase.estudiantes
Specifically, in:
def set_clase
#clase = Clase.find(params[:id])
#estudiantes_seleccionados = #clase.estudiantes
end
How do I use it (#estudiantes_seleccionados) in the selected: field in a collection_select in the view as to preselect the multiple values in the variable when loading the dropdown ?
<%= collection_select(:estudiantes, :id, Estudiante.all, :id, :id_campus, {selected: #estudiantes_seleccionados}, {class: 'form-control', multiple: 'true'}) %>
The problem seems to be multiple: 'true'. When I delete it, only one of the values of #estudiantes_seleccionados gets preselected in the dropdown, but when it is present, none of the values in the array appears.
So, how do I have all the values in #estudiantes_seleccionados the appear as preselected in the dropdown ?
Try the following:
<%= collection_select(:estudiantes, :id, Estudiante.all, :id, :id_campus, {selected: #estudiantes_seleccionados.map(&:id)}, {class: 'form-control', multiple: 'true'}) %>
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_elem_select_size
The select box I see is an area based select box as opposed to a drop down. However, In rails I can only get the drop down based select box. I have tried:
<%= f.collection_select :role_cont, Role.where(company: current_user.company), :name, :name, include_blank: true, :multiple => true, class: 'form-control chosen-it', :size => 10 %>
The above does not work. Note the :size attribute. From a gem I have:
<%= select_tag 'recipients', recipients_options(#chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
This does work, it creates an area based select box that I want. How do I set my select boxes to be a scroll able area as opposed to a simple drop down? Also I want to be able to use scroll able select box even when I can only select a singular object
Here why you added ':multiple => true' if you want select only one option,
these option is used when you want to select multiple values from select box.
<%= f.collection_select :role_cont, Role.where(company: current_user.company), :name, :name, include_blank: true, :multiple => true, class: 'form-control chosen-it', :size => 10 %>
Remove :multiple => true and it work as you want without area based select box
I'm using a form with a select multiple :true, it submits fine but on edit it does not properly select values.
f2.select :question_answer_multi, qd[:question_answer_options].split(','), {}, multiple: true, class: 'form-control'
This same setup works correctly with a single select, but not with my multiple select?
qd[:question_answer_options] provides a comma separated string entered by users on a different form used to generate this one.
Try the following code
<%= f2.select :question_answer_multi, options_for_select(qd[:question_answer_options].split(','), array_of_values_to_be_selected), {}, {class: 'form-control', multiple: true } %>
replace 'array_of_values_to_be_selected' with the array of values that needs to be pre selected in the edit page.
I was able to fix this by adding this into my model:
serialize :question_answer_multi
This saves the array in a format that Rails can read, allowing it to be called normally in the edit form like this:
f2.select :question_answer_multi, qd[:question_answer_options].split(','), {}, {class: 'form-control', multiple: true }
Try this one
<%= f2.select :question_answer_multi, qd[:question_answer_options].split(','), {:include_hidden => false},{ multiple: 'multiple', class: 'form-control'} %>
I have a simple_form input field that looks like this:
<%= f.input :particular_users, collection: #all_users, input_html: { class: 'multiselectuser', multiple: true} %>
When I leave multiple: true off, the form submits the chosen value for the parameter :particular_users and I can see the value when debugging using "raise params.inspect". However when I leave the multiple: true option there, no vales get passed for the parameter :particular_users.
What am I doing wrong?
EDIT: I can not use the association input because :particular_users is a virtual attribute and has no relationship. I want the multiple select box to pass whatever values that are in there, even if they are arbitrary.
f.input :days, collection: #your_collection, input_html: { multiple: true }
It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).
To create multiple select tags with simple_form, use:
<%= f.association :particular_users, collection: #all_users, input_html: { class: 'multiselectuser'} %>
see part Associations in the gem description.
But as you don't want to use an ActiveRecord associaion, use select_tag:
<%= select_tag 'particular_users',
options_from_collection_for_select(#all_users, :id, :name),
multiple: true, class: 'multiselectuser' %>
Couldn't find any reference to my problem in the API, so here goes:
<%= f.collection_select :category_id, #categories, :id,
:name, {prompt: true}, { selected: #selected_value } %>
My users arrive to the form from different links, and depending on the link, they get their categories pre-selected for them from the #categories set. Sometimes they come from a general page, so instead of a pre-selected option they see the default prompt.
Problem: with my current code prompt replaces the selected value.
Thanks for any advice!
So I went with placing a conditional inside my collection_select and now it works fine
<%= f.collection_select :category_id, #categories, :id, :name,
#selected_category ? {prompt: "Your text"} : {selected: #selected_category} %>