Rails options_from_collection_for_select with custom attributes for each option

You probably already know that you can use options_from_collection_for_select helper method generate <option></option> from a collection. If you are unaware of it, you can check this api documentation.

It helped me greatly until I needed some custom attributes to each <option></option> which will be used by jQuery for doing some tasks in UI level. Custom attributes will help me by reducing server calls for getting those information. However, I did not find a way set custom attributes with the above helper method. So, I’ve made a basic version of my own (if you prefer, you can override the original method). Put the following method in your helper file (may be in application_helper.rb).

Continue reading →