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' %>
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'}) %>
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 am creating a form has a drop down selection. I want to use two "text_method"s for the input but I am unsure how to do this. I want to include the year and name (both are two different columns in my rails model.
Here is what I have but it does not work:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year, :model_name, include_blank: true %>
Here is the official documentation- http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
Use this in your view:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year_and_name, include_blank: true %>
Add a method like this to your Bat model:
def model_year_and_name
"#{model_year} #{model_name}"
end
I have an edit form in erb.
<%= form_for #animal do |f| %>
Within the code I have a select with options:
<%= f.select :gender, options_for_select([['Mare'], ['Stallion'], ['Gelding']], :selected => :gender) %>
However, the select is not showing the correct selected value.
What could I be doing wrong?
I can get it to work if I hardcode it but of course that is not a viable option.
In your code, your options_for_select() call sets the selected value to "gender" and does not attempt to use the value from your form object.
Please see the docs for options_for_select() for usage examples.
options_for_select(['Mare', 'Stallion', 'Gelding'], f.object.gender)
options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.gender)
Alternatively, you can do this, which will already use the gender() value for your form object:
<%= f.select :gender, ['Mare', 'Stallion', 'Gelding'] %>
By the way, if you are using :include_blank => true, this will set your current selection to blank even though the form "knows" what is selected.
I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist.
Example:
<%= collection_select(:expense, :project_id, Project.all,
:id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %>
in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern.
Also, my form is on a controller and view called "reports", I am not putting this search in the default controllers.
Thanks!
Seems that this will work.
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", #search.project_id_eq) %>
If anyone has another suggestion, would love to know it too.
To do this with an include_blank, put it outside of the parentheses:
ex:
<%= f.select :languages_id_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>
== UPDATE ==
better yet, use f.collection_select. This way after the user searches for something using the drop down, that option is preselected on the following page:
<%= form.collection_select :vendor_id_eq, Vendor.all, :id, :name, include_blank: true %>