This time, I will introduce how to use map.
Basic form
Array variables= ["a", "b", "c"]
Array variables.map {|Variable name|Specific processing}
categories = ["a", "b", "c"] #Array
categories.map {|category| [category.name, category.id] } #Id to the element of the array,Give name and issue one by one
When actually using it, it is often used with select.
application_controller.rb
def set_category
@categories = Category.all
end
= form.select :category_id, @categories.map{ |category| [category.name, category.id] }, {prompt:"Please select"}
that's all
Recommended Posts