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 %>
Related
I have a Category Model. I want to let user select category for their post. The Model has only name. It's one model to many categories relationship.
Here is my code
<%= f.select :category, Category.all, :prompt => "Select One" %>
I learnt it from rails guy How to create Categories in Rails
I want to display the name but it shows some weird option.
Thanks in advance.
Try this i hope this will help.
<%= f.select :category, Category.all.collect{|c| [c.name, c.id]}, prompt: "Select One" %>
<%= f.select :category, Category.all.map{|c| [c.name, c.id]}, :prompt => "Select One" %>
Try this
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 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
I would like to use a select method in form_for in Rails. I'm not quite sure how to do it. Could anyone point me to an example?
Are you talking about select or select_tag or collection_select?
# advanced users only
= f.select :field_id, options_for_select(Model.collect{|m| [m.name, m.id]})
# easiest, assuming you have a model
= f.collection_select :field_id, Model.all, :id, :name
# without model
= select_tag 'model[field_id]', #model.field_id, options_for_select(Model.collect{|m| [m.name, m.id]})