Ruby on Rails: Select by Array Helper
02 Dec 2010
I wrote this little bit of RoR code to make it easy to make a select_array based on an active record object and then output it with the current one selected by default:
Use this code:
<%= select_array(@the_object.the_id_field,'the_object[the_id_field]','ObjectInActiveRecord') %>
To call this in application_helper.rb:
def select_array(the_id,the_name,the_model) items = eval(the_model).all.map { |item| [item.name, item.id] } select_tag the_name, options_for_select(items, the_id) end
Its a pretty simple way and makes a nice drop-select that you can use.