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} %>
Related
I have a collection_select as below. What I want is to display the prompt value on page load and not the id of the object. But now it displays the value of the id of the f object of the form on page load.
How can I change it so that as soon as the page loads the prompt value is displayed and the other id values are shown when the collection_select is clicked?
<%= f.collection_select(:id, Animal.all, :id, :id, {include_blank: 'Select animal'}, selected: params[:id]) %>
I tried this also:
<%= f.collection_select(:id, Animal.all, :id, :id, {include_blank: 'Select animal'}, selected: params[:id]) %>
This time Select animal is shown at the top when collection_select is clicked but the id of the present Animal object is displayed on page load. In the first case prompt value is not displayed anywhere. How can I change it?
You can try following for it,
<%= f.collection_select(:id, Animal.all, :id, :id, include_blank: 'Select animal', selected: Animal.find(params[:id])) %>
I have a drop down select that is set up like below:
<%= select_tag :city_id, option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name) %>
It works fine, except that when I load the edit view the list loads the first item in the select, not the saved value. Is there parameter I'm missing? On rails 4.
According to the documentation on option_groups_from_collection_for_select found here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select
It has a sixth parameter that is the selected value, so just add the last parameter with the value you want and it will work:
<%= select_tag :city_id,
option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name, "your_city") %>
Instead of using select_tag use select
# f being your form object
<%= f.select :city, option_groups_from_collection_for_select(#regions, :cities, :name, :id, :name) %>
Considering you have a valid association with city
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 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 %>
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..." %>