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 %>
Related
the relevant part of the new form looks like this:
<%= f.fields_for :event_artists do |fea| %>
<%= fea.collection_select :artist_id, Artist.all, "id", "name", {include_blank: true}, {multiple: true} %>
<% end %>
on the log, you can see that the first item of the array is always blank, even if I didn't select the blank field
"event_artists_attributes"=>{"0"=>{"artist_id"=>["", "2", "5"]}}}
is there a way to fix this? perhaps, make it so that if the blank field is selected, then no actual event_artists can be selected in that case, and vice versa?
The empty artist_id is important. On another form, you may have omitted the artist select altogether, in which case, the artists association should not be affected.
If the artist select is included, and you de-select all artists, the artists need to be removed from the artists association. Normal HTML behavior would not include the artist_id parameter in the PUT at all when nothing is selected. Your controller in that case would think that you do not want to modify the artists association at all.
To solve this, the collection_select includes a hidden field with a blank value to let the controller know that the form intends to alter the artists association. If no artists have been selected, that blank element in the array will ensure all artists are removed from the association.
I think the problem has to do with the order of arguments. I looked at collection_select and tried the following code in an app I'm working on.
<%= collection_select(:category, :category_id, Category.all, :id, :name, {}, {multiple: true}) %>
UPDATE:
<%= fea.collection_select :artist_id, Artist.all, "id", "name", {prompt: true}, {multiple: true} %>
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} %>
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.
The code I am using for the dropdown list is this:
<%= f.select :post_type_id, option_groups_from_collection_for_select(#categories, :post_types, :name, :id, :name) %>
It neatly divides the options into optgroups.
But how do I modify the code to include a prompt (or a default value) of "Please select..." ?
It seems hard to do with grouped dropdowns.
(The rails docs seem to suggest using a hash, but I've tried several alternatives without success.)
Bah, right after I posted the question I found the answer was in the docs for select, and not under option_groups_from_collection_for_selectdocs where I had been looking.
The answer is:
<%= f.select :post_type_id, option_groups_from_collection_for_select(#categories, :post_types, :name, :id, :name), :include_blank => "Please select..." %>