This time, I will explain how to use collection_select.
I want to give an id to each element in the select and display it by name. Like this.
collection_select has options, HTML attributes, etc., and you can change various appearances by changing them, but this time I will add an empty option "Please select" at the beginning, which is the most orthodox. I would like.
The model is like this.
= f.collection_select column name,Array of elements,Item of value attribute,Text item, { prompt: "Please select" }
It looks like this when actually used.
= f.collection_select :prefecture, @prefecture, :id, :name, { prompt: "Please select" }
#@Put all prefecture information in prefecture (instance variable)
#Since I want to give an id with the value of the element, the item part of the value attribute is:to id
#I want to display by name in the text, so the item part of the text is:to name
Basically, collection_select is often used in parallel with active_hash. It may be more efficient to study active_hash and then cellection_select.
https://qiita.com/Yu-8chan/items/9f19e2495937f4e36dde
https://railsdoc.com/page/collection_select
that's all
Recommended Posts