Creating an array of key-value pairs for options_for_select

My situation is:

  • I’m using a select_tag form helper which shows a menu of names from
    a collection in a view
  • options_for_select works nicely here because I can just pass the
    collection in

so, it looks like this

<%= select_tag( :group_selection, options_for_select(@group_names))
%>

problem:

some of the strings in @group_names are too wide for the panel I want
to display the menu in

  • so, I thought I’d truncate the names for display, but I still need
    the full names so that the selection passed back to the controller in
    params[:group_selection] will match the actual name of the group

what’s cool is that options_for_select will take an array of key-value
pairs, so I can put the truncated name in the first as the key and the
original as the value and it works… when I hard code up an array as
a test

I want to write a helper function that would take @group_names and a
truncation length and would return the array of key-value pairs
described above… and that would be passed into options_for_select.

This might be a simple ruby problem, but I havent found how to write
the code to do.

Any help will be much appreciated.