Dynamic Select Lists in Rails
Nov 14
I’ve come to find out that Rails has pretty good support for dynamic select lists. This is achieved through different form helpers, one of which is called collection_select. You can read more about collection_select here. The usage is simple and the best part of this is that you get pre-select functionality for free. To clarify, let’s say you’re adding a product to your web store and associating it with a category called “books”. The next time you go to edit the product the “books” category will be pre-selected in the drop down menu. This can be achieved through the following code:
collection_select(:product, :category_id, @categories, :id, :name)
Instead of duplicating content I will let you read more about the parameters by going here. Once again Rails, thank you for making my life simpler and letting me focus on solving more important problems.
