I’ve always wondered what do you guys do for filling out dropdown menus in rails, and not have the code splattered in the view. Do you guys make a pivot table? Do you make a class and add methods to return arrays?
I always wonder how other people make them work, for example the other way I need to fill a combo box with all the countries I made a class called DropDownFiller, and added a method called fill_countries that would return an array with all the countries.
What are the best practices regarding this or how do you do it?
The helper
options_for_selecttakes an array of options and builds the select. From the docs:There are more detailed examples in the documentation.
Depending on how your data is set up, it can be easy to fill a list. For example:
or for custom values
Something else I’ve seen done a few times is defining a helper method in the model that returns the correct values: