I am using simple form gem and I am wondering how do you do a collection_select ? I know you can do a select, but how do you get the values from a collection?
So, for example, how would you do this in a simple form:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)
Thanks
You would do it like this:
collection_select(:post, :author, Author.all, :id, :name_with_initial)
Take a look at SimpleForm - Collection Select Input
Related
Using the simple_form gem, I've a form with the following field:
f.association :company, label_method: :company_name, value_method: :id, include_blank: false
How can I sort the resulting array by a attribute, like :name? The docs only mention using order for another case, using collection: .... Just adding order: :name to my input doesn't work.
Is this possible at all? Thanks!
You can specify the ordering of the collection items like this:
f.association :company, collection: Company.all.order(:name), label_method: :name, value_method: :id
I don't think there is another way to sort the displayed items, you have to use collection.
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 can't understand the difference between the two. Can someone please explain the difference when using form_for?
Say you have this: <%= form_for(#post) do |f| %>
Examples- When would you use this?
<%= collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
vs. use this?
<%= f.collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
Rails Api
When you use f.some_form_helper the helper will already know the name of the model you want to make the field name for. This way you can drop that :post argument. form_for(#post) gives you the f form builder object that knows what model the form is for.
With the regular collection_select (or any other helper with f.) you have to pass in, as the first argument, the name of the model the field is for.
Your example is a bit off because you passed in the same arguments to both. f.collection_select doesn't need the :post.
This is correct use of the non f. helper:
<%= collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true) %>
This is a corrected way to use the f. helper:
<%= f.collection_select(:author_id, Author.all, :id, :name_with_initial, prompt: true) %>
the f object has a reference back to the model you passed in to form_for via f.object. This is how it knows to call collection_select(:post, ...) under the hood.
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 want to sort / order it (desc or asc how i want it) by the database table column "plays"
Im totally confused. Just found a solution for select but not collection_select?
some code of my view
<%= f.collection_select :player1, Player.all, :id, :name %>
dont know how to sort / order
there are also columns in the database table like "plays", "goals" ...
Just pass actually ordered collection to collection_select helper:
collection_select(:post, :author_id, Author.order('created_at DESC'), :id, :name_with_initial, :prompt => true)
So, in your source example it will look like this:
<%= f.collection_select :player1, Player.order('plays DESC'), :id, :name %>